import UIKit
import SDWebImage
import AFNetworking
import TalkFunBarrageRenderer

public class TFLargeLive: UIViewController ,UIScrollViewDelegate,TalkfunSDKLiveDelegate,TFLotteryViewControllerDelegate,TFMyLotteryViewControllerDelegate,TFVoteViewDelegate,TFHornViewDelegate,TFChatTableViewControllerDelegate,TFNetWorkSelectViewDelegate,TFHorizontalChatViewDelegate,TalkfunExaminationViewDelegate,TF_CourseDownLoadDetailDelegate,TF_SignInDelegate,TalkfunPreventCheatCountdownViewDelegate{
    @objc public var access_token:String = ""
    @objc public var Titles:String = ""
    @objc public var startTime:String = ""
    let ButtonViewHeight:CGFloat = 40
    let superView = UIView()
    let talkfunSDK = TalkfunSDKLive.shareInstance()
    let pptView = TFLargeLivePPTview()
    let cameraView = TFCameraView()
    let buttonView = TFButtonView()
  
    let scrollView = UIScrollView()
    let shadowView = UIView()
    //网络状态
    var networkStatus:TalkfunNetworkStatus = TalkfunNetworkStatusGood
    //横竖屏   true: 横屏  false:竖屏
    var isOrientationLandscape:Bool = false
    
    @objc public var TF_isStatusBarHidden:Bool = false
    //是否是桌面分享
    var isDesktop:Bool = false
    //老师直播端的摄像头状态
    var cameraShow:Bool = false

    //互动区域
    var chatContrller = TFChatTableViewController()
    //提问
    var questionContrller = TFQuestionTableViewController()
    //**奖品名称*/
    var prize_name:String = ""
    /**图片下载管理者*/
    let imageManage = SDWebImageManager.shared
    //投票详情选择
    let voteSelectVC = TFVoteSelectView.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
    //表情
    let emotionView = TFEmotionView()
    //是否点击了摄像头
    var isvideofull:Bool = false
    var voteSuccesstimer:Timer?
    var netSelectIndex:NSInteger = 0
    var isAbleChat:Bool = false
    //跳转到了课件显示
    var pushCourseDownDetailController:Bool = false
    //蒙版
    let maskView = UIView()
    //水印
    var layer:CATextLayer?
    var layerTimer:Timer?

    //弹幕
    let barrageRender = TalkFunBulletView()
   // 设置红包投票状态标签
    let setCenter = TFLargeLiveSetCenter()
    //无网络 断网
    var isfalseWork:Bool = false
    //横屏刘海
    var isOrientationiPhoneX:CGFloat = 9
    //状态栏
    let statusBar = TFLargeLive.statusBarView()
    //底部刘海屏view
    let OrientationBottomView = UIView()
    //考试
    let examView = TalkfunExamView()
    //在线人数
    let onlineLabel = TFMoveLabel()

    private var preventCheatCountdown = TalkfunPreventCheatCountdown()

    
    //滚动条
    private var _hornView: TFHornView?
    private var hornView: TFHornView? {
        set {
            _hornView = newValue
       }
       get {
            if _hornView == nil {
                _hornView = TFHornView()
                _hornView!.frame = CGRect(x: 0, y: self.pptView.frame.maxY-36, width: self.pptView.frame.width, height: 36)
                _hornView!.isHidden = true
                _hornView!.delegate = self

           }
          return _hornView
      }

        
    }
    //更多视图
    private var _moreView: TFMoreView?
    private var moreView: TFMoreView? {
        set {
            _moreView = newValue
       }
       get {
            if _moreView == nil {
                _moreView = TFMoreView()
                _moreView!.frame = CGRect(x: 0, y: self.superView.frame.height, width: self.superView.frame.width, height: 130)
            _moreView!.announcementBtn.addTarget(self, action: #selector(self.announcebtnClick), for: .touchUpInside)
                _moreView!.networkSelectBtn.addTarget(self, action: #selector(self.networkSelectbtnClick), for: .touchUpInside)
                _moreView!.courseDownloadBtn.addTarget(self, action: #selector(self.courseDownloadBtnClick), for: .touchUpInside)
        
                _moreView!.backgroundColor = UIColor.white

           }
          return _moreView
      }
    }
    //网络状态
     let networkSelectView = TFNetWorkSelectView()

    //公告
    private var _announcementView: TF_AnnounceMentView?
    private var announcementView: TF_AnnounceMentView?
    {
        set {
            _announcementView = newValue
       }
       get {
        if _announcementView == nil
           {
                
            _announcementView = TF_AnnounceMentView()
            _announcementView!.backgroundColor = UIColor.clear
            _announcementView!.clipsToBounds = true
            _announcementView!.layer.cornerRadius = 10
            _announcementView!.alpha = 0
            
            let announcementViewW:CGFloat = 352
            let announcementViewH:CGFloat = 172 + 10
            let announcementViewX:CGFloat = (self.superView.frame.width - announcementViewW)/2
            let announcementViewY = self.buttonView.frame.maxY + 10
        _announcementView!.frame = CGRect(x: announcementViewX, y: announcementViewY, width: announcementViewW, height: announcementViewH)
        }
           return _announcementView
       }
    }
    //输入时的透明背景黑色
    private var _inputBackgroundView: UIView?
    private var inputBackgroundView: UIView?
    {
        set {
            _inputBackgroundView = newValue
       }
       get {
            if _inputBackgroundView == nil {
                _inputBackgroundView = UIView.init(frame: self.pptView.pptsFunctionView.frame)
            _inputBackgroundView!.backgroundColor = UIColor.black.withAlphaComponent(0.5)
            _inputBackgroundView!.isHidden = true
            }
           return _inputBackgroundView
       }
    }
    /**抽奖开始*/
    private var _lotteryVC: TFLotteryViewController?
    private var lotteryVC: TFLotteryViewController? {
         set {
             _lotteryVC = newValue
        }
        get {
             if _lotteryVC == nil {
                 _lotteryVC = TFLotteryViewController()
                 _lotteryVC!.view.frame = self.superView.frame
                 _lotteryVC!.delegate = self
                 _lotteryVC!.deleteBtn.addTarget(self, action:#selector(self.deleteBtnClicked), for: .touchUpInside)

            }
           return _lotteryVC
       }
    }
    /**显示中奖人*/
    private var _myLotteryVC: TFMyLotteryViewController?
    private var myLotteryVC: TFMyLotteryViewController?
    {
        set {
            _myLotteryVC = newValue
       }
       get {
            if _myLotteryVC == nil {
                _myLotteryVC = TFMyLotteryViewController()
                _myLotteryVC!.view.frame = self.superView.frame
                _myLotteryVC!.delegate = self
           }
          return _myLotteryVC
      }
    }
     /**中奖人列表*/
    private var _listLotteryVC: TFLotteryWinnerListViewController?
    private var listLotteryVC: TFLotteryWinnerListViewController?
        {
            set {
                _listLotteryVC = newValue
           }
           get {
                if _listLotteryVC == nil {
                    _listLotteryVC = TFLotteryWinnerListViewController()
                    _listLotteryVC!.view.frame = self.superView.frame
                    _listLotteryVC!.closeBtn.addTarget(self, action: #selector(self.closelistLotterVC), for: .touchUpInside)
               }
              return _listLotteryVC
          }
        }

    //投票列表
    private var _votelistVC: TFVoteListView?
    private var votelistVC: TFVoteListView?
    {
        set {
            _votelistVC = newValue
       }
       get {
            if _votelistVC == nil {
                _votelistVC = TFVoteListView()
                
            _votelistVC!.backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
            _votelistVC!.frame = CGRect(x: 0, y: self.superView.frame.height, width: self.superView.frame.width, height: 462)
                
            let maskLayer = self.getMaskLayer(_votelistVC!.bounds)
            _votelistVC!.layer.mask = maskLayer
                _votelistVC!.closeBtn.addTarget(self, action: #selector(self.closeVoteList), for: .touchUpInside)

            _votelistVC!.delegate = self
           }
          return _votelistVC
      }
    }
    //投票成功
    private var _voteSuccessView: UIView?
    private var voteSuccessView: UIView?
    {
        set {
            _voteSuccessView = newValue
       }
       get {
            if _voteSuccessView == nil {
                _voteSuccessView = UIView()
    if (self.isOrientationLandscape) {
        _voteSuccessView!.frame = CGRect(x: self.superView.frame.width-375, y: 9, width: 375, height: self.superView.frame.height)

    } else {
        _voteSuccessView!.frame = CGRect(x: 0, y: self.superView.frame.height-462, width: self.superView.frame.width, height: 462)
        
    }
    _voteSuccessView!.backgroundColor = UIColor.white
    _voteSuccessView!.layer.shadowColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.1).cgColor

    _voteSuccessView!.layer.shadowOffset = CGSize(width: 0, height: -2)
              
    _voteSuccessView!.layer.shadowRadius = 12
    _voteSuccessView!.layer.shadowOpacity = 1

    let maskLayer = self.getMaskLayer(_voteSuccessView!.bounds)

    _voteSuccessView!.layer.mask = maskLayer

    let  img = UIImageView()
    img.image = UIImage.init(named: "HTLive.bundle/Default page_vote")
       
    img.frame = CGRect(x:(_voteSuccessView!.frame.size.width-201)/2.0, y: (_voteSuccessView!.frame.size.height-150)/2.0, width: 201, height: 150)
                
    _voteSuccessView!.addSubview(img)
 
    let  closeBtn = UIButton()
   closeBtn.setImage(UIImage.init(named: "HTLive.bundle/close_gray"), for: .normal)
   
    closeBtn.frame = CGRect(x: _voteSuccessView!.frame.size.width-10-32, y: 10, width: 32, height: 32)
 closeBtn.addTarget(self, action: #selector(self.closeVoteSuccess), for: .touchUpInside)
    _voteSuccessView!.addSubview(closeBtn)

    let lab = UILabel()
    lab.frame = CGRect(x: 0, y: img.frame.maxY-28, width: _voteSuccessView!.frame.size.width, height: 22)
           
    lab.textAlignment = .center
    lab.text = "投票成功"
                lab.textColor = UIColor(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1)
 
               
    lab.font = UIFont.PingFang(size: 16)
    _voteSuccessView!.addSubview(lab)

           }
          return _voteSuccessView
      }
    }
    //链接弹出框
    private var _urlAlertView: TFUrlAlertView?
    private var urlAlertView: TFUrlAlertView?
    {
        set {
            _urlAlertView = newValue
       }
       get {
            if _urlAlertView == nil {
                 
                _urlAlertView = TFUrlAlertView()
                if (self.isOrientationLandscape)
                {
                   _urlAlertView!.frame = CGRect(x: self.superView.frame.width-375, y: 0, width: 375, height: self.superView.frame.height)
                           } else {
                               _urlAlertView!.frame = CGRect(x: 0, y: self.superView.frame.height-287, width: self.superView.frame.width, height: 287)
                          
                }
               
                _urlAlertView!.backgroundColor = UIColor.white
                _urlAlertView!.commitBtn.addTarget(self, action: #selector(self.alertUrlCommitUI), for: .touchUpInside)
                let maskLayer = self.getMaskLayer(_urlAlertView!.bounds)
                _urlAlertView!.layer.mask = maskLayer
                _urlAlertView!.closeBtn.addTarget(self, action: #selector(self.url_closeBtnClick), for: .touchUpInside)
                _urlAlertView!.closeBtn.addTarget(self, action: #selector(self.url_closeBtnClick), for: .touchUpInside)
                _urlAlertView!.cancelBtn.addTarget(self, action: #selector(self.url_closeBtnClick), for: .touchUpInside)

            }
           return _urlAlertView
      }
    }
    //横屏  更多  设置中心
    private let horMoreView =  TFHorizontalMoreView()
    
    //横屏提问记录
     let horQuestionView = TFHorizontalQuestionView()
    //竖的输入栏
    let chatTFView = TFTextFieldView()
    //横屏的输入栏
    let horChatView = TFHorizontalTextFieldView()

    //横屏聊天区域
    private var _horChatController: TFHorizontalChatView?
    private var horChatController: TFHorizontalChatView?
    {
        set {
            _horChatController = newValue
       }
       get {
            if _horChatController == nil
           {
                _horChatController = TFHorizontalChatView()
                _horChatController!.backgroundColor = UIColor.clear
            _horChatController!.delegate = self
                _horChatController!.frame = CGRect(x:0, y:self.superView.frame.height-21-192-self.isOrientationiPhoneX, width:320, height:192)
            _horChatController!.isHidden = true
            _horChatController!.tapGestueBlock = {[weak self] in
                guard let `self` = self else { return }
                self.pptView.pptsFunctionView.isHidden = !self.pptView.pptsFunctionView.isHidden
                self.horChatView.isHidden = !self.horChatView.isHidden
                self.view.endEditing(true)
                self.OrientationBottomView.isHidden = !self.OrientationBottomView.isHidden
                self.showEmotionview(show: false)
                self.horChatView.biaoqingBtn.isSelected = false

            }
          
         }
           return _horChatController
        }
    }
    //签到
    let signInView = TF_SignIn()
   
    //主播回复view
    private var _replyView: UIView?
    private var replyView: UIView?
    {
            set {
                _replyView = newValue
           }
           get {
                if _replyView == nil
               {
                    _replyView = UIView()
                    _replyView?.backgroundColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 0.7)
       
                    _replyView!.frame = CGRect(x: self.superView.frame.size.width-124, y: self.superView.frame.size.height-self.isOrientationiPhoneX-16-32-56, width: 124, height: 32)
          
                    let maskPath = UIBezierPath.init(roundedRect: self.replyView!.bounds, byRoundingCorners:[UIRectCorner.topRight,UIRectCorner.topLeft], cornerRadii: CGSize(width: 14, height: 14))
                    
                                let maskLayer = CAShapeLayer()
                                 maskLayer.frame = self.replyView!.bounds
                    maskLayer.path = maskPath.cgPath
                    self.replyView!.layer.mask = maskLayer
                    
                
                    
                        let tap =  UITapGestureRecognizer.init(target: self, action: #selector(self.replyViewClick))
                        _replyView?.isUserInteractionEnabled = true
                        _replyView?.addGestureRecognizer(tap)
                             
                                let lab = UILabel()
                                lab.text = "老师回复了你"
                                lab.textColor = UIColor.white
                                lab.font = UIFont.PingFang(size: 14)
                                _replyView?.addSubview(lab)
                              
                                lab.frame = CGRect(x: 8, y: 6, width: 84, height: 20)
                                let img = UIImageView()
                                img.image = UIImage.init(named: "HTLive.bundle/smallAcc")
                                img.frame =  CGRect(x: 96, y: 10, width: 12, height: 12)
                                _replyView?.addSubview(img)
                             
                    
                }
               return _replyView
          }
               
    }
    //快速答题
    private var _answerCard: TF_AnswerCardView?
    private var answerCard: TF_AnswerCardView?
    {
            set {
                _answerCard = newValue
           }
           get {
                if _answerCard == nil
               {
                    _answerCard = TF_AnswerCardView()
                    _answerCard!.frame = CGRect(x: (self.superView.frame.size.width-325)/2.0, y: (self.superView.frame.size.height-200)/2.0, width: 325, height: 200)
                
                    _answerCard!.backgroundColor = UIColor.white
                   //灰色阴影
                    _answerCard!.layer.shadowOffset = CGSize(width: 1, height: 1)
                    _answerCard!.layer.shadowOpacity = 0.3
                    _answerCard!.layer.shadowColor = UIColor.lightGray.cgColor
                    //快速答题 提交
                    _answerCard!.fastPaperDetailsSelectBlock = { [weak self] (subjectId,answerList,access_key,msg) in
                        guard let `self` = self else { return }
                        let model = TFFillExaminationAnswersParam()
                           model.subjectId = subjectId
                           model.answerList = answerList
                           model.access_key = access_key as String
                           model.type = TFExaminationTypeQuickAnswer
                        
                        self.talkfunSDK.fillExaminationAnswers(model){[weak self] (code, error) in
                            guard let `self` = self else { return }
                    
                           if (code == 0)
                            {
                               self.answerCardSubmitSuccessful()
                           }
                            else {
                                self.TF_toast( error!.msg )
                             
                           }
                        
                          }
                        
                    }
                    _answerCard!.closeBlock = {[weak self] in
                        guard let `self` = self else { return }
                        self.removeAnserCard()
                       
                    }
                    _answerCard!.endBlock = {[weak self] in
                        guard let `self` = self else { return }
                        self.endAnswerCardView()
                       
                    }
            
          }
               return _answerCard
        }
    
    }
    //课件下载
     let courseDownView = TF_CourseDownLoadView()

    
    public func pptViewTouchesBeganUI()
    {
        self.tapClick()
        self.pptView.pptsFunctionView.isHidden = !self.pptView.pptsFunctionView.isHidden
        if self.isOrientationLandscape {
            self.horChatView.isHidden = self.pptView.pptsFunctionView.isHidden
        }else
        {
            self.horChatView.isHidden = true
        }
       
    }
    // MARK: - 初始化UI
    public override func viewDidLoad() {
        super.viewDidLoad()
        
        
        self.TF_isStatusBarHidden = UIApplication.shared.isStatusBarHidden
        self.portrait()

        self.pptView.touchesBegan = {[weak self] in
            guard let `self` = self else { return }
            self.pptViewTouchesBeganUI()
        }
        
        var superViewY:CGFloat = 24
           if self.isNotchScreen {
               superViewY = 44
           }
       let view = UIView()
        view.backgroundColor = UIColor.white
        view.frame = CGRect(x: 0, y: superViewY, width: self.getCalculateViewFrame(self.isOrientationLandscape).size.width, height: self.getCalculateViewFrame(self.isOrientationLandscape).size.height-superViewY)
        self.view.addSubview(view)
        
        self.superView.frame = self.getCalculateViewFrame(self.isOrientationLandscape)
        self.superView.backgroundColor = UIColor.clear
        

        self.view.addSubview(self.superView)
        self.view.addSubview(self.chatTFView)
        
        
        self.horChatView.frame = CGRect(x: 0, y: self.superView.frame.height-56-self.isOrientationiPhoneX, width: self.superView.frame.width, height: 56)
        self.horChatView.isHidden = true
        self.horChatView.placeholder = "说点什么吧..."
        self.horChatView.buttonsAddTarget(target: self, action: #selector(self.pptsFunctionButtonClicked))
        self.view.addSubview(self.horChatView)
        
//        self.chatTFView.keyboardWillShowBlock = {[weak self] height in
//            guard let `self` = self else { return }
//            
//            if self.chatTFView.textView.isFirstResponder {
//                //正在输入
//                var newY:CGFloat = 0
//                if height>0 {
//                    newY = -height + self.isOrientationiPhoneX
//                }
//                self.superView.frame = CGRect(x: 0, y: newY, width: self.getCalculateViewFrame(self.isOrientationLandscape).width, height: self.getCalculateViewFrame(self.isOrientationLandscape).height)
//            }else
//            {
//                //结束输入 回到默认状态
//                self.superView.frame = self.getCalculateViewFrame(self.isOrientationLandscape)
//            }
//           
//          
//        }
        
        self.initCreateUI()
        if (self.access_token.count>0) {
            self.configViewWithAccessToken(access_token: self.access_token)
        }
        else
        {
            self.TF_toast( "token不能为空")
          
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+2) {
                
                if(self.navigationController != nil)
                {
                    self.navigationController?.popViewController(animated: true)
                }else{
                    self.dismiss(animated: true)
                }
            }
     
        }
        
        self.onlineLabel.isHidden = true
        self.onlineLabel.font = UIFont.PingFang(size: 10)
        self.onlineLabel.frame = CGRect(x: self.view.frame.size.width - 80, y: 8, width: 65, height: 25)
        self.onlineLabel.backgroundColor = UIColor(red:35/255.0 , green: 39/255.0, blue: 57/255.0, alpha: 1)
        self.onlineLabel.textColor = UIColor(red:236/255.0 , green: 255.0/255.0, blue: 255.0/255.0, alpha: 1)
        self.onlineLabel.textAlignment = .center
        self.onlineLabel.layer.masksToBounds = true
        self.onlineLabel.layer.cornerRadius = 12.5
        self.onlineLabel.alpha = 0.5
        self.scrollView.addSubview(self.onlineLabel);
        
    }
    public func luckyBagUI()
    {
        //添加福袋
//        let luckyBag = TFluckyBag()
    }

  public func getPptViewY()->CGFloat
    {
        var pptViewY:CGFloat = 0
        //刘海屏与是竖屏
        if (self.isNotchScreen) {
            if (self.isOrientationLandscape==false) {
                pptViewY = 44
            }
             
        }else{
            if (self.isOrientationLandscape==false) {
                pptViewY = 20
            }
        }
        return pptViewY
    }
    //    #pragma mark - 强制横屏
    public func orientationLandscape()
    {
        self.chatTFView.isHidden = true

        self.orientationPortrait(portrait: false)
        self.updateChrysanthemum()
    }
    //MARK:显示更多视图
    public func showMoreview(show:Bool,animation:Bool)
    {
        var duration = (self.superView.frame.height - 130.0) / self.superView.frame.height / 2.0
        if animation==false {
            duration = 0
        }
        UIView.animate(withDuration: duration) {
            if (show) {
                //显示
                self.moreView!.frame = CGRect(x: 0, y: self.getCalculateViewFrame(self.isOrientationLandscape).height-130, width: self.superView.frame.width, height: 130)

                self.chatTFView.isShowMoreView = true

            } else {
                //隐藏
                self.moreView!.frame = CGRect(x: 0, y:self.getCalculateViewFrame(self.isOrientationLandscape).height, width: self.superView.frame.width, height: 130)

                self.chatTFView.isShowMoreView = false
                self.chatTFView.moreButton.isSelected = false
            }
            
            self.chatTFView_frame()
        }
    
    }
    
    
    //TODO: 横竖屏切换--------------------------------
    public func orientationPortrait(portrait:Bool)
    {

        self.isOrientationLandscape = !portrait
        self.pptView.pptsFunctionView.isOrientationLandscape = !portrait
        self.scrollView.isHidden = !portrait
        

        if (self.isOrientationLandscape) {

                //强制翻转屏幕，Home键在右边。
        UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
        
        UIViewController.attemptRotationToDeviceOrientation()
            
        UIApplication.shared.isStatusBarHidden = true
            
        }else{
            
        UIApplication.shared.isStatusBarHidden = false
          
        self.portrait()

        }
        
        
        self.superView.frame = self.getCalculateViewFrame(self.isOrientationLandscape)
        self.horMoreView.frame = self.superView.bounds
        //需要改变状态栏状态，就调用 setNeedsStatusBarAppearanceUpdate 方法：
        setNeedsStatusBarAppearanceUpdate()
        
        
        let pptViewY = self.getPptViewY()
        if (portrait) {//转为竖屏幕
           //TODO:ppt容器（4：3比例自适应）
            let pptViewW:CGFloat = self.superView.frame.width
            self.pptView.frame = CGRect(x: 0, y: pptViewY, width: pptViewW, height: pptViewW*0.75)
        } else {
            self.pptView.frame = CGRect(x: 0, y: pptViewY, width: self.superView.bounds.size.width, height: self.superView.bounds.size.height-pptViewY)
                
        }
        self.barrageRender.frame = self.pptView.bounds
        
        let  model = TFBitmapModel()
        model.status = self.talkfunSDK.liveStatus
        model.startTime = NSString.init(format: "%ld", self.talkfunSDK.roomInfo.startTime)
        //是否无网线
        model.isNOWork = self.isfalseWork
        model.isFull = self.isOrientationLandscape
        model.imgStr = self.talkfunSDK.roomInfo.zhuboInfo.avatar as NSString
        //  当前时间 大于   开始时间课程
        if (TalkfunSharedTools.getCurrentDateString()>self.talkfunSDK.roomInfo.startTime) {
        //主播暂时不在
        model.isTimeStart = false
        }else{
        ////日期内容
        model.isTimeStart = true
        }
        self.pptView.bitmapView.updateLiveStatus1(model)
        self.inputBackgroundView!.frame = self.pptView.bounds
        //改变喇叭滚动条的frame
        self.hornView!.frame = CGRect(x: 0, y: self.pptView.frame.maxY-36, width: self.pptView.frame.width, height: 36)
        
        if (UIDevice.current.userInterfaceIdiom == .pad) {
            if portrait {
                self.cameraView.frame = CGRect(x: self.pptView.frame.width*0.7, y: self.pptView.frame.maxY+self.ButtonViewHeight, width: self.pptView.frame.width*0.3, height: self.pptView.frame.width*0.3*0.75)
            }else
            {
                let cameraViewW = self.pptView.frame.width*0.3
                let cameraViewY = (self.pptView.frame.height - (cameraViewW*0.75))/2
                let cameraViewX = self.pptView.frame.width-cameraViewW-100

                self.cameraView.frame = CGRect(x: cameraViewX, y: cameraViewY, width: cameraViewW, height: cameraViewW*0.75)
            }
      
        }else{
            var frame = self.cameraView.frame
            if portrait {
                frame.origin = CGPoint(x: self.pptView.frame.width-120, y: self.pptView.frame.maxY+self.ButtonViewHeight)
              
            }else
            {
                frame.origin = CGPoint(x: self.pptView.frame.maxX-self.cameraView.frame.size.width, y: self.pptView.frame.maxY-50-frame.height)
           
            }
            self.cameraView.frame = frame
        }
        
        let announcementViewW:CGFloat = 352
        let announcementViewH:CGFloat = 172 + 10
        let announcementViewX:CGFloat = (self.superView.frame.width - announcementViewW)/2
        
        self.networkSelectView.frame = self.superView.bounds
        
        if self.courseDownView.isShow {
            self.courseDownView.isShow = false
            self.courseDownView.isHidden = true
        }
        self.courseDownView.frame = self.superView.bounds
       
       
        if (portrait) {  //Yes为竖屏,false为横屏
        
            self.announcementView!.isHidden = false
            self.pptView.pptsFunctionView.more_Btn.isHidden = true
            self.pptView.pptsFunctionView.heng_refreshBtn.isHidden = true
            
            self.showHorMoreView(show: false)
            self.showMoreview(show: false,animation: false)

            self.votelistVC!.frame = CGRect(x: 0, y: self.superView.frame.height, width: self.superView.frame.width, height: 462)
            self.voteSelectVC.frame = self.votelistVC!.frame
            self.urlAlertView!.frame = CGRect(x: 0, y: self.pptView.frame.height-287, width:  self.pptView.frame.width, height: 287)

            self.horChatView.isHidden = true
            self.pptView.pptsFunctionView.bottomView.isHidden = false
            self.horChatController!.isHidden = true
            //改变喇叭滚动条的frame
            self.hornView!.frame = CGRect(x: 0, y: self.pptView.frame.maxY-36, width: self.pptView.frame.width, height: 36)
   
            self.setCenter.frame = CGRect(x: self.superView.frame.width-50, y: self.pptView.frame.maxY+40, width: 50, height: self.superView.frame.height-self.pptView.frame.maxY-40-56)
          
            
            self.statusBar.isHidden = false
            
           
            self.OrientationBottomView.isHidden = true

           
        } else {
           
            self.pptView.pptsFunctionView.more_Btn.isHidden = false
            self.pptView.pptsFunctionView.heng_refreshBtn.isHidden = false
        
           
 
            let announcementViewY = (self.superView.frame.size.height-announcementViewH)/2.0
            self.announcementView!.frame = CGRect(x: announcementViewX, y: announcementViewY, width: announcementViewW, height: announcementViewH)
            self.votelistVC!.frame = CGRect(x: self.superView.frame.size.width, y: 0, width: 375, height: self.superView.frame.size.height)
            self.voteSelectVC.frame = self.votelistVC!.frame
            self.urlAlertView!.frame = CGRect(x: self.pptView.frame.width-375, y: 0, width: 375, height: self.pptView.frame.height)
            self.horChatView.isHidden = false
            self.pptView.pptsFunctionView.bottomView.isHidden = true
            self.horChatController!.isHidden = false
            //改变喇叭滚动条的frame
            self.hornView!.frame = CGRect(x: 0, y: 45, width: self.pptView.frame.width, height: 36)
            self.setCenter.frame = CGRect(x: self.pptView.frame.width-80, y: 45, width: 50, height: self.pptView.frame.height-65-45)
           
            self.statusBar.isHidden = true
            
            self.OrientationBottomView.isHidden = false
        }
        
        if (self.talkfunSDK.isLoadCourseware && self.cameraShow) {
            self.cameraView.isHidden = false
            self.horChatView.chuangkou_Btn.isHidden = false
        } else {
            self.cameraView.isHidden = true
            self.horChatView.chuangkou_Btn.isHidden = true
        }
        if portrait {
            self.buttonView.frame = CGRect(x: 0, y: self.pptView.frame.maxY-1, width: self.pptView.frame.width, height: self.ButtonViewHeight)
        }else
        {
            if self.cameraView.isHidden {
                self.buttonView.frame = CGRect(x: self.pptView.frame.maxX, y: 0, width: self.superView.frame.width-self.pptView.frame.width, height: self.ButtonViewHeight)
                
            }else
            {
                self.buttonView.frame = CGRect(x: self.pptView.frame.maxX, y: self.cameraView.frame.maxY, width: self.superView.frame.width-self.pptView.frame.width, height: self.ButtonViewHeight)
            }
            
        }
      
        //修改scrollView的frame和contentSize
        var announcementViewY = self.buttonView.frame.maxY + 10
        if portrait {
            self.scrollView.frame = CGRect(x: 0, y: self.pptView.frame.maxY+self.ButtonViewHeight, width: self.superView.frame.width, height: self.superView.frame.height - self.pptView.frame.maxY - self.ButtonViewHeight)
            
            
        }else
        {
            self.scrollView.frame = CGRect(x: self.pptView.frame.maxX, y: self.buttonView.frame.maxY, width:  self.superView.bounds.size.width - self.pptView.frame.maxX, height: self.superView.bounds.size.height-self.buttonView.frame.maxY)
            
             announcementViewY = (self.superView.frame.height-announcementViewH)/2
           
        
        }
        self.announcementView!.frame = CGRect(x: announcementViewX, y: announcementViewY, width: announcementViewW, height: announcementViewH)
        self.scrollView.contentSize = CGSize(width: self.scrollView.frame.width*2, height: self.scrollView.frame.height)
        
        self.shadowView.frame = self.scrollView.frame
        self.chatContrller.view.frame = CGRect(x: 0, y: 0, width: self.scrollView.frame.size.width, height: self.scrollView.frame.size.height-56-self.isOrientationiPhoneX)
     
        self.questionContrller.view.frame = CGRect(x: self.scrollView.frame.size.width, y: 0, width: self.scrollView.frame.size.width, height: self.scrollView.frame.size.height-56-self.isOrientationiPhoneX)
        
        //修改chatTF和askTF的frame
        var chatTFViewH:CGFloat = 56
        if self.chatTFView.frame.height>chatTFViewH {
            chatTFViewH = self.chatTFView.frame.height
        }
        
        self.chatTFView.frame = CGRect(x: 0, y: self.superView.frame.height-chatTFViewH-self.isOrientationiPhoneX, width: self.scrollView.frame.width, height: chatTFViewH)

        var horChatViewH:CGFloat = 56
        if self.horChatView.frame.height>horChatViewH {
            horChatViewH = self.horChatView.frame.height
        }
        self.horChatView.frame = CGRect(x: 0, y: self.superView.frame.height-horChatViewH-self.isOrientationiPhoneX, width: self.superView.frame.width, height: horChatViewH)
        
        self.OrientationBottomView.frame = CGRect(x: 0, y: self.superView.frame.height-self.isOrientationiPhoneX, width: self.superView.frame.width, height: self.isOrientationiPhoneX)
        
        self.chatTFView.takeChatandAsk()
        self.horChatView.takeChatandAsk()
  
        self.horQuestionView.frame = self.superView.bounds
        
        self.horChatController!.frame = CGRect(x: 0, y: self.superView.frame.height-21-192-self.isOrientationiPhoneX, width: 320, height: 192)
        self.emotionView.frame = CGRect(x: 0, y: self.superView.frame.height, width: self.superView.frame.width, height: self.ExpressionViewHeight())
  
        if (portrait) { //更新cellwidth
            self.chatContrller.tableView.reloadData()
            self.questionContrller.tableView.reloadData()
          
        } else {
            self.horChatController!.tableView.reloadData()
            self.horQuestionView.tableView.reloadData()
          
        }
        
        if (self.voteSuccessView != nil) { //防止投票成功就切换横竖屏
            self.voteSuccessView!.removeFromSuperview()
        }
        self.replyView!.frame = CGRect(x: self.superView.frame.size.width-124, y: self.superView.frame.size.height-self.isOrientationiPhoneX-16-32-56, width: 124, height: 32)
        
        self.signInView.frame = CGRect(x: (self.superView.frame.width-240/2), y: (self.superView.frame.height-312)/2.0, width: 240, height: 312)
        
        if self.horMoreView.isHidden == false {
            self.horMoreView.showView(false)
        }
      
        
        self.maskView.frame = self.superView.bounds
        self.lotteryVC?.view.frame = self.superView.bounds
        self.lotteryVC?.updateFrame()
    
        self.myLotteryVC?.view.frame =  self.superView.bounds
        self.myLotteryVC?.updateFrame()
  
        self.listLotteryVC?.view.frame = self.superView.bounds
        self.listLotteryVC?.updateFrame()
        
        self.updateChrysanthemum()
        self.examView.frame = UIScreen.main.bounds
    }
    public func ExpressionViewHeight()->CGFloat
    {
        var expressionViewHeight:CGFloat = 100
        if UIDevice.current.userInterfaceIdiom == .pad {
            expressionViewHeight = 100+20
        } else {
            expressionViewHeight = 170+60
        }
        return expressionViewHeight
    }
    //横屏显示
    public func fullScreen()
    {
        self.view.endEditing(true)
        self.myLotteryVC!.view.removeFromSuperview()
        self.myLotteryVC = nil
        self.lotteryVC!.view.removeFromSuperview()
        self.lotteryVC = nil
        self.listLotteryVC!.view.removeFromSuperview()
        self.listLotteryVC = nil
        if (self.isOrientationLandscape==false) {
            //进入全屏
            self.pptView.pptsFunctionView.fullScreenBtn.isSelected = true
            self.pptView.pptsFunctionView.fullScreenBtn.setImage(UIImage.init(named: "HTLive.bundle/mask_icon_nrmal"), for: .normal)
            
            self.orientationLandscape()
            self.buttonView.isHidden = true
            
        }else{
            // 退出全屏
            self.pptView.pptsFunctionView.fullScreenBtn.isSelected = false
            self.pptView.pptsFunctionView.fullScreenBtn.setImage(UIImage.init(named: "HTLive.bundle/mask_icon_max"), for: .normal)
            
            self.orientationPortrait()
            self.buttonView.isHidden = false
        }
    }
    
    public func updateChrysanthemum()
    {
        if (self.talkfunSDK.isExchanged) {
            self.activityIndicator.frame = self.pptView.bounds
            self.pptView.addSubview(self.activityIndicator)
         
        }else{
            self.activityIndicator.frame = self.cameraView.bounds
            self.cameraView.addSubview(self.activityIndicator)
          
        }
    }
    
    //MARK:显示网络选择
    public func showNetworkSelectview(_ isShow:Bool)
    {
        self.networkSelectView.isShow = isShow
        self.networkSelectView.showView(isShow)


    }
    public func fullScreenUI()
    {
        if (self.isvideofull) {
            self.talkfunSDK.exchangePPTAndCameraContainer()
            self.cameraView.isHidden = false
            self.isvideofull = false
          }
        self.fullScreen()
          
    }

    public func portrait()
    {
        UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")

        
        UIViewController.attemptRotationToDeviceOrientation()
    }
    public func quit()
    {
        self.preventCheatCountdown.remind.stopUI()

        
        UIApplication.shared.isStatusBarHidden = self.TF_isStatusBarHidden
        
        setNeedsStatusBarAppearanceUpdate()
        
        self.statusBar.removeFromSuperview()
        
        self.horChatView.destroySharedIntance()
        self.chatTFView.destroySharedIntance()
        self.questionContrller.removeFromParent()
        
        self.chatContrller.removeFromParent()
       
        NotificationCenter.default.removeObserver(self)
    
        self.pushCourseDownDetailController = false

        self.talkfunSDK.destroy()

        self.setCenter.examinationBtn.timerInvalidate()
        self.examView.examinationView.timerInvalidate()
        self.examView.testPaperDetails.timerInvalidate()
        self.examView.introductionToTestPapers.timerInvalidate()

        //弹幕view删除
        self.barrageRender.bulletSwitch = false

        self.barrageRender.removeFromSuperview()


        if self.voteSuccesstimer != nil {
            self.voteSuccesstimer?.invalidate()
            self.voteSuccesstimer = nil
        }
        if self.layerTimer != nil {
            self.layerTimer?.invalidate()
            self.layerTimer = nil
        }
        //返回上一页
        self.TF_dismissUI()
 
    }
    @objc public func  pptsFunctionButtonClicked(btn:UIButton)
    {
        let returnButton:NSInteger =  200
        let statusSwitchingButton:NSInteger = 202
        let refreshButton:NSInteger = 203
        let fullScreenButton:NSInteger = 206
        let switchLineButton:NSInteger = 211
        let moreButtonClick:NSInteger = 2001
        let chatButtonClick:NSInteger = 2002
        let danmuButtonClick:NSInteger = 2003
        let biaoqingButtonClick:NSInteger = 2004
        let askAndchatButtonClick:NSInteger = 2005
        let chuankouButtonClick:NSInteger = 2006
        //返回按钮
        if (btn.tag == returnButton) {
            if (self.isOrientationLandscape) { //横屏
                self.fullScreen()
            } else {
                
                let alert = UIAlertController.init(title: "温馨提示", message: "确定要退出直播间吗", preferredStyle:.alert)
                let cancel:UIAlertAction = UIAlertAction.init(title: "取消", style: .default) { action in
                    
                }
                alert.addAction(cancel)
                let otherAction:UIAlertAction = UIAlertAction.init(title: "确认", style: .default) { action in
                    self.quit()
                }
                alert.addAction(otherAction)
                if  UIDevice.current.model.hasPrefix("iPad") {
                    alert.popoverPresentationController?.sourceView = self.superView
                    alert.popoverPresentationController?.sourceRect = CGRect(x: 0, y: 0, width: 1.0, height: 1.0)
                }
                
                self.present(alert, animated: true)

            }
        }
        //切换ppt和camera
        else if (btn.tag == statusSwitchingButton)
        {
            if (self.cameraView.isHidden) {
                return
            }
            self.talkfunSDK.exchangePPTAndCameraContainer()
        
            self.updateChrysanthemum()
        }
        //刷新
        else if (btn.tag == refreshButton){

            
            self.talkfunSDK.reload()
        }
        //全屏按钮
        else if (btn.tag == fullScreenButton){
            self.fullScreenUI()
         
        }
        else if (btn.tag == switchLineButton) {
            //线路
            self.showNetworkSelectview(true)
        
          
        } else if (btn.tag == moreButtonClick) {
            self.showHorMoreView(show: true)

        } else if (btn.tag == chatButtonClick) { // 聊天
            btn.isSelected = !btn.isSelected
            if (btn.isSelected) {
                self.horChatController!.isHidden = true
            } else {
                self.horChatController!.isHidden = false
            }
        } else if (btn.tag == danmuButtonClick) { // 弹幕
            btn.isSelected = !btn.isSelected
        
            if (btn.isSelected) {
                self.barrageRender.bulletSwitch = false
            } else {
                self.barrageRender.bulletSwitch = true
            }
        } else if (btn.tag == biaoqingButtonClick) { // 表情
            btn.isSelected = !btn.isSelected
            if (btn.isSelected) {
                self.view.endEditing(true)
               
            }
            //横屏表情点击
            self.showEmotionview(show: btn.isSelected)
        } else if (btn.tag == askAndchatButtonClick) { //聊天互动
            btn.isSelected = !btn.isSelected
            self.horChatView.textView.text = ""
            self.horChatView.takeChatandAsk()
      
            if (self.horChatView.biaoqingBtn.isSelected) {
                self.horChatView.biaoqingBtn.isSelected = false
                self.showEmotionview(show: false)
              
            }

        } else if (btn.tag == chuankouButtonClick) { //窗口
            btn.isSelected = !btn.isSelected
            
            if (btn.isSelected) {
                self.cameraView.isHidden = true
            } else {
                self.cameraView.isHidden = false
            }
        }
    }
    
   @objc public func backVoteList()
    {
        self.showVoteSelect(false)
    }
    public func initCreateUI()
    {
        let pptViewY = self.getPptViewY()
       //TODO:ppt容器（4：3比例自适应）
        let pptViewW:CGFloat = self.superView.frame.width
        self.pptView.frame = CGRect(x: 0, y: pptViewY, width: pptViewW, height: pptViewW*0.75)
       self.pptView.backgroundColor = UIColor(red:35/255.0 , green: 39/255.0, blue: 57/255.0, alpha: 1)
        self.superView.addSubview(self.pptView)
        
        self.barrageRender.bulletSwitch = true
        self.barrageRender.tf_height = 136
        self.pptView.addSubview(self.barrageRender)
        self.barrageRender.frame = self.pptView.bounds
        self.barrageRender.isUserInteractionEnabled = false
        
        self.scrollView.frame = CGRect(x: 0, y: self.pptView.frame.maxY+self.ButtonViewHeight, width: self.superView.frame.width, height: self.superView.frame.height - self.pptView.frame.maxY - self.ButtonViewHeight)
        
        self.scrollView.delegate = self
        self.scrollView.backgroundColor = UIColor.white
        
        //-------------
        self.shadowView.frame = self.scrollView.frame
     
        self.buttonView.buttonsAddTarget(self, action: #selector(self.btnViewButtonsClicked))
        self.buttonView.frame = CGRect(x: 0, y: self.pptView.frame.maxY-1, width: self.superView.frame.width, height: self.ButtonViewHeight)
    
        self.buttonView.selectButton(buttonView.chatBtn)

        self.emotionView.backgroundColor = UIColor.white
        self.emotionView.expressionBlock = { [weak self] expressionName in
          guard let `self` = self else { return }
            if (self.isOrientationLandscape) {
                if (expressionName=="delete") {
                    
                    //删除一位
                    self.horChatView.textView.text = TalkfunSharedTools.GetTextMessage(self.horChatView.textView.text! as NSString) as String
                    
                  
                } else if (expressionName=="send") {  //发送
                    if (self.horChatView.textView.text.count > 0) {
                        self.sendClick(msg: self.horChatView.textView.text! as NSString)
                      
                        self.horChatView.biaoqingBtn.isSelected = false
                     
                    }
                }
                else {
                    
                    
                    self.horChatView.textView.text = NSString.init(format: "%@[%@]", self.horChatView.textView.text,expressionName) as String
                    
                    self.horChatView.udateFrame(height: self.emotionView.frame.size.height, textView: self.horChatView.textView)
             
                }
            } else {
                if (expressionName=="delete") {
                    
                    self.chatTFView.textView.text = TalkfunSharedTools.GetTextMessage(self.chatTFView.textView.text! as NSString) as String
                   
                   
                } else if (expressionName=="send") {  //发送
                    if (self.chatTFView.textView.text.count > 0) {
                        self.sendClick(msg: self.chatTFView.textView.text! as NSString)
                     
                        self.chatTFView.expressionButton.isSelected = false
                       
                    }
                }
                else {
                    self.chatTFView.textView.text = NSString.init(format: "%@[%@]", self.chatTFView.textView.text! as NSString,expressionName) as String
                    
                    self.chatTFView.udateFrame(height: self.emotionView.frame.height, textView: self.chatTFView.textView)
                    

                }
            }
           
        }
  
        self.voteSelectVC.backgroundColor = UIColor.white
        self.voteSelectVC.frame = CGRect(x: 0, y: self.superView.frame.height, width: self.superView.frame.width, height: 462)
        self.voteSelectVC.closeBtn.addTarget(self, action: #selector(self.closeVoteSelect), for: .touchUpInside)
        self.voteSelectVC.backBtn.addTarget(self, action: #selector(self.backVoteList), for: .touchUpInside)
        
        let maskLayer = self.getMaskLayer(self.voteSelectVC.bounds)
        self.voteSelectVC.layer.mask = maskLayer
        self.voteSelectVC.submit.addTarget(self, action: #selector(self.submitVote), for: .touchUpInside)

        //ppt上面的按键事件
        self.pptView.pptsFunctionView.buttonsAddTarget(self, action: #selector(self.pptsFunctionButtonClicked))
        self.pptView.pptsFunctionView.titleLab.text = self.Titles

       //TODO:cameraView容器
        self.cameraView.frame = CGRect(x: self.superView.frame.width-120, y: self.pptView.frame.maxY+self.ButtonViewHeight, width: 120, height: 120*0.75)
      
        if (UIDevice.current.userInterfaceIdiom == .pad ) {
            let cameraViewW = self.pptView.frame.width*0.3
            self.cameraView.frame = CGRect(x: self.superView.frame.width*0.7, y: self.pptView.frame.maxY+self.ButtonViewHeight, width: cameraViewW, height: cameraViewW*0.75)
            
       }
       self.cameraView.backgroundColor = UIColor.black
       self.cameraView.isHidden = true
       self.cameraView.videoExchangeBtn.addTarget(self, action: #selector(self.exchangePPTClick), for: .touchUpInside)

           //添加聊天控制器
        self.createScrollView()

        //TODO:设置中心
        self.setCenter.frame = CGRect(x: self.superView.frame.width-50, y: self.pptView.frame.maxY+40, width: 50, height: self.superView.frame.height-self.pptView.frame.maxY-40-56)
        
        self.superView.addSubview(self.setCenter)
        self.setCenter.voteButton.addTarget(self, action: #selector(self.voteClick), for: .touchUpInside)
        self.setCenter.lotteryCall.addTarget(self, action: #selector(self.lotteryClick), for: .touchUpInside)
        //TODO:点名中事件
        self.setCenter.rollCall.addTarget(self, action: #selector(self.rollCallClick), for: .touchUpInside)
        //考试入口
        self.setCenter.examinationBtn.btn.addTarget(self, action: #selector(self.examinationBtnClick), for: .touchUpInside)

        self.horQuestionView.frame = self.superView.bounds
        self.horQuestionView.backgroundColor = UIColor.clear
        self.horQuestionView.closeBtn.addTarget(self, action: #selector(self.closeQuestionRecord), for: .touchUpInside)
        self.view.addSubview(self.horQuestionView)
        

        self.view.addSubview(self.cameraView)
        //视频在聊天输入下面
        self.view.addSubview(self.chatTFView)
        self.view.addSubview(self.horChatView)
        
        
       
        self.networkSelectView.frame = self.superView.bounds
        self.networkSelectView.closeBtn.addTarget(self, action: #selector(self.networkSelectClose), for: .touchUpInside)
        self.networkSelectView.delegate = self
        self.networkSelectView.backgroundColor = UIColor.clear
        self.view.addSubview(self.networkSelectView)
        
        
        //课件下载
        self.courseDownView.frame = self.superView.bounds
        self.courseDownView.backgroundColor = UIColor.white
        self.courseDownView.type = .large
        self.courseDownView.downLoadRoot.delegate = self
        self.view.addSubview(self.courseDownView)
        
        
        self.pptView.pptsFunctionView.insertSubview(self.inputBackgroundView!, at: 0)
        self.view.addSubview(self.moreView!)

        self.superView.addSubview(self.voteSelectVC)
    
        self.superView.addSubview(self.horChatController!)
        
        self.OrientationBottomView.backgroundColor = UIColor(red: 0/255.0 , green: 0/255.0 , blue: 0/255.0 , alpha: 0.4)
    
        self.OrientationBottomView.isHidden = true
        self.superView.addSubview(self.OrientationBottomView)
        
        
        self.horMoreView.gonggaoBtn.addTarget(self, action: #selector(self.announcebtnClick), for: .touchUpInside)
        self.horMoreView.netWorkBtn.addTarget(self, action: #selector(self.networkSelectbtnClick), for: .touchUpInside)
        self.horMoreView.askRecordBtn.addTarget(self, action: #selector(self.horQuestionRecord(btn:)), for: .touchUpInside)
        self.horMoreView.courseDownloadBtn.addTarget(self, action: #selector(self.courseDownloadBtnClick), for: .touchUpInside)
        self.horMoreView.frame = self.superView.bounds
        self.view.addSubview(self.horMoreView)

        //TODO:考试
        //试卷列表代理
        self.examView.examinationView.delegate = self
        self.examView.examinationView.close.addTarget(self, action: #selector(self.examinationViewClose), for: .touchUpInside)
        self.examView.frame = self.superView.bounds
        self.view.addSubview(self.examView)

        
        //填写试卷
        self.examView.testPaperDetails.testPaperDetailsSelectBlock = {[weak self] (subjectId,answerList,access_key,msg) in
            
            guard let `self` = self else { return }
            
            let model = TFFillExaminationAnswersParam()
            model.subjectId = subjectId
            //表单
            if (msg.length>0) {
                model.type = TFExaminationTypeTable
            }else{
                //随堂考
                model.type = TFExaminationTypeTestPaper
            }

            model.answerList = answerList
            model.access_key = access_key as String
            self.talkfunSDK.fillExaminationAnswers(model) { code, error in
                
            }
     
        }
      
        //提交试卷
        self.examView.testPaperDetails.submitTestPaperBlock = {[weak self] (access_key,ID) in
            guard let `self` = self else { return }
            let model = TFExaminationSubmitParam()
            model.access_key = access_key as String
            self.talkfunSDK.examinationSubmit(model) { code, error in
                //刷新详情
                self.getQuestionnaireDetailedUI(ID: ID, isShow: false)
                //刷新试卷列表
                self.getQuestionnaireListUI(isShow: false)
            }
        }
        
        //TODO:签到View
        self.signInView.delegate = self
        self.signInView.frame = self.superView.bounds
        self.signInView.view.frame = CGRect(x: 0, y: self.signInView.frame.size.height, width: self.signInView.frame.size.width, height: 312)
        self.superView.addSubview(self.signInView)
        //TODO:确定签到事件
        self.signInView.round.addTarget(self, action: #selector(self.roundClick), for: .touchUpInside)
        
       //监听
        self.registerEventListener()
    }
    //TODO: 发起提问
    @objc public func  QuestionCick(text:NSString)
    {
        self.talkfunSDK.enterAskQuestions(text as String) {[weak self] (code, model, error) in
            guard let `self` = self else { return }
            
            if (code==0 ) {
                self.chatContrller.update_Question(model: model!)
                self.questionContrller.update_Question(model: model!)
                self.horQuestionView.update_Question(model: model!)
                self.horChatController!.update_Question(model: model!)
            } else {
                self.TF_toast( error!.msg )
              
            }
        }
      
    }
    //加载数据弹幕
    public func loadData(msg:NSString)
    {
        let tempColor = UIColor.white
        self.barrageRender.tf_init(withContent: msg as String, ontOfSize: 14, textColor: tempColor, backgroundColor: UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.6))

    }
   public func addModel(model:TF_LivingChatData)
    {
        self.chatContrller.update_Chat(model: model)
        self.horChatController!.update_Chat(model: model)
    }
    public func addChatData(chatData:TFChatData)
    {
        let imageURL:NSString =  chatData.imageURL as NSString
        
        if (imageURL.length>0) {
            
            self.imageManage.loadImage(with: URL.init(string: imageURL as String), options: SDWebImageOptions.retryFailed) { receivedSize, expectedSize, targetURL in

            } completed: {[weak self] image, data, error, cacheType, finished, imageURL in
                guard let `self` = self else {
                    return
                }

                DispatchQueue.main.async {

                    if (image != nil)
                    {
                        
                        let model = TalkfunSharedTools.getLivingChatData(dict: chatData)
                        model.image = image!
                        self.addModel(model: model)
                       
                    }
                }
            }

        }else{
            self.addModel(model: TalkfunSharedTools.getLivingChatData(dict: chatData))
          
        }
    }
    //TODO: 发起聊天
    @objc public func  sendClick(msg:NSString)
    {
        //直播未开始
        if self.talkfunSDK.roomInfo.liveStatus == TalkfunLiveStatusWait && self.talkfunSDK.roomInfo.mod_beforeclass_live.config.chat.enable == false {
            self.TF_toast("课前禁止聊天互动")
            return
        }
        if self.talkfunSDK.roomInfo.liveStatus == TalkfunLiveStatusStop && self.talkfunSDK.roomInfo.mod_afterclass_live.config.chat.enable == false {
            self.TF_toast("课后禁止聊天互动")
            return
        }

        self.talkfunSDK.sendChat(msg as String) { [weak self] (code, model, error) in
            guard let `self` = self else { return }
            if (code == 0 ) {
                self.addChatData(chatData: model!)
                self.loadData(msg: model!.msg as NSString)
               
                self.chatTFView.superHeight = 0
                self.horChatView.superHeight = 0
                self.chatTFView.textView.text = ""
                self.horChatView.textView.text = ""
                self.horChatView.isupadteTetxview = false
                self.showEmotionview(show: false)
                self.horChatView.takeChatandAsk()
                self.chatTFView.takeChatandAsk()
            
            }else{
                self.chatTFView.superHeight = 0
                self.horChatView.superHeight = 0
                self.chatTFView.textView.text = ""
                self.horChatView.textView.text = ""
                self.horChatView.isupadteTetxview = false
                self.showEmotionview(show: false)
                self.horChatView.takeChatandAsk()
                self.chatTFView.takeChatandAsk()
                self.TF_toast( error!.msg )
                
            }
            
            let chatTFViewH:CGFloat = 56
          
            self.chatTFView.frame = CGRect(x:0, y: self.superView.frame.height-chatTFViewH-self.isOrientationiPhoneX, width: self.scrollView.frame.width, height: chatTFViewH)
            
            self.horChatView.frame = CGRect(x: 0, y: self.superView.frame.height-56-self.isOrientationiPhoneX, width: self.superView.frame.width, height: 56)
        }
    }
    public func createScrollView()
    {
        self.superView.addSubview(self.scrollView)

        self.isOrientationiPhoneX = 0
        
        if (self.isNotchScreen) {

            self.isOrientationiPhoneX = 21
        }

        /*=================加tableView===================*/
            self.chatContrller.view.frame = CGRect(x: 0, y: 0, width: self.scrollView.frame.size.width, height: self.scrollView.frame.size.height-56-self.isOrientationiPhoneX)
            self.chatContrller.view.backgroundColor = UIColor(red: 242/255.0, green: 242/255.0, blue: 242/255.0, alpha: 1)
            self.chatContrller.view.tag = 300
            let tap = UITapGestureRecognizer.init(target: self, action:#selector(self.tapClick))
            self.chatContrller.view.isUserInteractionEnabled = true
            self.chatContrller.view.addGestureRecognizer(tap)
            self.chatContrller.delegate = self
            self.scrollView.addSubview(self.chatContrller.view)
            self.addChild(self.chatContrller)
            self.chatContrller.createUI()
            self.scrollView.isPagingEnabled = true

        
            //提问
            self.questionContrller.view.frame = CGRect(x: self.scrollView.frame.size.width, y: 0, width: self.scrollView.frame.size.width, height: self.scrollView.frame.size.height-56-self.isOrientationiPhoneX)
            self.questionContrller.view.tag = 300 + 1
            self.questionContrller.view.backgroundColor = UIColor(red: 242/255.0, green: 242/255.0, blue: 242/255.0, alpha: 1)
            let tap1 = UITapGestureRecognizer.init(target: self, action:#selector(self.tapClick))

            self.questionContrller.view.isUserInteractionEnabled = true
            self.questionContrller.view.addGestureRecognizer(tap1)
            self.scrollView.addSubview(self.questionContrller.view)
        self.addChild(self.questionContrller)
            self.questionContrller.createUI()

        self.scrollView.contentSize = CGSize(width: self.scrollView.frame.width*2, height: self.scrollView.frame.height)
  
        /*=================添加textfield===================*/
        var chatTFViewH:CGFloat = 56
        if self.chatTFView.frame.height>chatTFViewH {
            chatTFViewH = self.chatTFView.frame.height
        }
        
        self.chatTFView.frame = CGRect(x:0, y: self.superView.frame.height-chatTFViewH-self.isOrientationiPhoneX, width: self.scrollView.frame.width, height: chatTFViewH)

        self.chatTFView.moreButton.addTarget(self, action: #selector(self.moreBtnClicked), for: .touchUpInside)
        self.chatTFView.expressionButton.addTarget(self, action: #selector(self.expressionBtnClick), for: .touchUpInside)
        self.chatTFView.askButton.addTarget(self, action: #selector(self.askBtnClick), for: .touchUpInside)

        self.chatTFView.sendBlock = { [weak self] textView in
            guard let `self` = self else { return }
            let num:NSInteger = NSInteger(self.scrollView.contentOffset.x/self.scrollView.frame.size.width)
  
            if (num == 0) {
        
                if (self.chatTFView.askButton.isSelected) {
                    self.QuestionCick(text: textView.text! as NSString)
    
                } else {
                    self.sendClick(msg: textView.text! as NSString)
                    
                }
            } else if (num == 1) {
                self.QuestionCick(text: textView.text! as NSString)
            }
            self.chatTFView.textView.text = ""
  
           
        }

        //加btnView
        self.superView.addSubview(self.buttonView)
        self.superView.addSubview(self.announcementView!)


        self.emotionView.frame = CGRect(x:0, y: self.superView.frame.height, width: self.superView.frame.width, height: self.ExpressionViewHeight())
     

        self.maskView.backgroundColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.5)

        self.maskView.isHidden = true
        let maskTap = UITapGestureRecognizer.init(target: self, action:#selector(self.maskViewClickTap))
    
        self.maskView.isUserInteractionEnabled = true
        self.maskView.addGestureRecognizer(maskTap)
        self.superView.addSubview(self.maskView)
  
        self.horChatView.sendBlock = { [weak self] textView in
            guard let `self` = self else { return }

            if (self.isAbleChat) {
                self.QuestionCick(text: textView.text! as NSString)
            } else {
                if (self.horChatView.askAndChat_Btn.isSelected) {
                    self.QuestionCick(text: textView.text! as NSString)
                } else {
                    self.sendClick(msg: textView.text! as NSString)
                }
            }
               
            self.horChatView.textView.text = ""
           
        }
        
        self.horChatView.clickBlock = { [weak self] (isClick) in
            guard let `self` = self else { return }
            if (isClick) {
                self.showEmotionview(show: false)
                self.horChatView.biaoqingBtn.isSelected = false
        }
    }
    }
    public func configViewWithAccessToken(access_token:String)
    {
        //配置房间参数
        let params = TalkfunSDKLiveInitParams()
        params.accessToken = access_token
        params.courseType = TalkfunCourseTypeInteractiveLive

        //TODO:初始化SDK-----------------
        //进入房间
        self.talkfunSDK.enterRoom(params)
        //开启画中画功能
//        self.talkfunSDK.picture(inPicture: true)
        //设置代理对象
        self.talkfunSDK.delegate = self
        //进入后台是否暂停（默认是暂停）
        self.talkfunSDK.setPauseInBackground(true)
        //3.把ppt容器给SDK（要显示摄像头区域的必须部分）
        self.talkfunSDK.configureCameraContainerView(self.cameraView.view)
        //2.把ppt容器给SDK（要显示ppt区域的必须部分）
        self.talkfunSDK.configurePPTContainerView(self.pptView)
    }

    //代理-------
    public func marqueeClick() {
        if (self.hornView!.link.length > 4) {
            self.urlAlertView!.urlLab.text = self.hornView!.link as String
            self.superView.addSubview(self.urlAlertView!)
            self.maskView.isHidden = false
        }
        
    }
    //选择线路
    public func setNetwork(_ operatorID: NSString, selectedSegmentIndex: NSInteger) {
        
        self.talkfunSDK.setNetwork(operatorID as String, selectedSegmentIndex: selectedSegmentIndex)
        
        self.netSelectIndex = selectedSegmentIndex
        self.showNetworkSelectview(false)
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.4) {
        self.TF_toast( String.init(format: "已切换到线路%ld", self.netSelectIndex+1) )
        
        }
    }
    
    public func operateModel(_ ID: NSInteger) {
        self.getQuestionnaireDetailedUI(ID: ID, isShow: true)
    }
    //TODO: 签到倒计时结束 TF_SignInDelegate
    public func signEnd() {
        self.setCenter.rollCall.isHidden = true
        self.setCenter.layoutSubviews()
    }

    //关闭考试列表
   @objc public func examinationViewClose() {
        self.examView.backgroundColorView.isHidden = true
        self.examView.examinationView.closeClick()
    }
    //快速答题卡倒计时结束
    public func endAnswerCardView()
    {
        self.answerCard?.frame = CGRect(x: (self.superView.frame.size.width-125)/2, y: (self.superView.frame.size.height-44)/2, width: 125, height: 44)

        self.answerCard!.countdownEndView.isHidden = false
        self.answerCard!.answerView.isHidden = true
        self.answerCard!.answerSuccessView.isHidden = true
        self.answerCard!.startAnswerCardSuccessTimers()

    }
    public func removeAnserCard()
    {
        self.answerCard!.removeFromSuperview()
        self.answerCard = nil
    }
   @objc public func answerCardSubmitSuccessful()
    {
        self.answerCard!.frame = CGRect(x: (self.superView.frame.width-150)/2.0, y: (self.superView.frame.height-150)/2.0, width: 150, height: 150)
        self.answerCard!.answerView.isHidden = true
        self.answerCard!.answerSuccessView.isHidden = false
        self.answerCard!.countdownEndView.isHidden = true
        self.answerCard?.startAnswerCardSuccessTimers()
   
    }

    //#pragma mark 主动点击获取列表
    @objc public func examinationBtnClick()
    {
        self.getQuestionnaireListUI(isShow: true)
    }
    //  主动获取考试列表
    @objc public func getQuestionnaireListUI(isShow:Bool)
    {
        let params = TFMemberParams()
            params.page = 1
        self.examView.backgroundColorView.isHidden = !isShow
        self.examView.examinationView.reload(isShow: isShow)
        if (isShow) {
            self.progressHUDShow()
        }
        self.talkfunSDK.getQuestionnaireList(params) {[weak self] (code, result, error) in
            guard let `self` = self else { return }
            if (isShow) {
                self.progressHUDDismiss()
            }
            if ( code == 0 ) {
                self.examView.examinationView.addData(result)
                self.setCenter.examinationBtn.addData(result)
            }
        }
    }
  //  #pragma mark 问卷详情
    @objc public func getQuestionnaireDetailedUI(ID:NSInteger,isShow:Bool)
    {
        
        let param = TTFExaminationDetailParam()
        param.id = ID
        param.type = TFExaminationTypeTestPaper
        self.examView.testPaperDetails.reload(isShow: isShow)
        if (isShow) {
            self.progressHUDShow()
        }
        self.talkfunSDK.getExaminationDetail(param) {[weak self] code, result, error in
            guard let `self` = self else { return }
            if (isShow) {
                self.progressHUDDismiss()
            }
            if (code == 0 ) {
                //详情页面
                self.examView.testPaperDetails.addData(result)
                //列表页
                self.examView.examinationView.reloadAnswerStatus(result.subjectExamineAnswerInfo)
              
            }
        }
    }
    public func progressHUDShow()
    {

       
    }
    public func progressHUDDismiss()
    {

    }
//    #pragma mark TalkfunExaminationViewDelegate 列表view的详情代理
//    #pragma mark 考试开关
    public func configExaminationUI()
    {
        if(self.talkfunSDK.roomInfo.mod_zhuboaction_live.config.examination.enable)
        {
            self.getQuestionnaireListUI(isShow:false)
        }
    }
//    #pragma mark 简介内部点击了详情
    public func answerNowClick()
    {
        self.operateModel(self.examView.introductionToTestPapers.examinationAddResult.id)
    }

//    #pragma mark 发布问卷
    public func  onExaminationPush(_ result: TFQuestionnairePushResult) {
  
        let t = result.endTime
        if (result.type == TFExaminationTypeQuickAnswer) { //快速答题卡
            
            let params = TTFExaminationDetailParam()
            params.id = result.id
            params.type = TFExaminationTypeQuickAnswer
            self.talkfunSDK.getExaminationDetail(params) {[weak self] code, result1, error in
                guard let `self` = self else { return }
       
                if (code == 0) {
                    self.view.addSubview(self.answerCard!)
                    self.answerCard?.setAnswerCard(result1)
              
                    self.answerCard!.answerCardId = result.id
                    self.answerCard!.addTimer(t)
                  
                }
            }
        } else if (result.type == TFExaminationTypeTestPaper || result.type == TFExaminationTypeTable) { //随堂考  表单
            let examination = TalkfunQuestionnaire()
            examination.id = result.id
            examination.title = result.title
            examination.endTime = result.endTime
            examination.answerStatus = TalkfunAnswerStatusUnsubmitted
            examination.subjectTotal = result.subjectTotal
            examination.type = result.type
            //添加到考试列表
            self.examView.examinationView.addExamination(examination)
            //入口button
            let new = TalkfunQuestionnaire()
            new.endTime = result.endTime
            new.id = result.id
            new.answerStatus = TalkfunAnswerStatusUnsubmitted
            self.setCenter.examinationBtn.addModel(new)
    
        }
    }
//    #pragma mark 主播提前-结束作答随堂测试
    public func  onExaminationEnd(_ result: TFQuestionnaireEndResult) {

        //快速答题卡
        if ((self.answerCard != nil)&&result.type == TFExaminationTypeQuickAnswer) {
            if (result.id == self.answerCard!.answerCardId) {
                self.endAnswerCardView()
            }
        } else if (result.type == TFExaminationTypeTestPaper || result.type == TFExaminationTypeTable) {
            self.examView.ID = result.id
            //根据情况弹窗
            self.examView.examinationEnd(model: result, target: self, action: #selector(self.seeDetails))
            
            //当前详情id == 结束ID
            if ((self.examView.testPaperDetails.model != nil)&&self.examView.testPaperDetails.model!.subjectExamineInfo.id == result.id) {
                self.examView.testPaperDetails.model!.subjectExamineInfo.isEndOfTestPaper = true
            }
            //列表 页面
            self.getQuestionnaireListUI(isShow: false)
        
            //入口 00:00:00
            let new = TalkfunQuestionnaire()
            new.id = result.id
            self.setCenter.examinationBtn.deleteUI(new)
    
        }


    }
//    #pragma mark 查看详情
    @objc public func seeDetails(btn:UIButton)
    {
         btn.superview?.isHidden = true
         btn.superview?.removeFromSuperview()
        if (!(btn.titleLabel!.text == "知道了")) {
            //详情 页面
            self.getQuestionnaireDetailedUI(ID: self.examView.ID, isShow: true)
        }
    }
//    #pragma mark 删除发布状态的随堂测试
    public func  onExaminationDelete(_ result: TFQuestionnaireDeleteResult) {
        
        if((self.examView.testPaperDetails.model != nil)&&self.examView.testPaperDetails.model!.subjectExamineInfo.id == result.id )
        {
            /**2:随堂考 3.问卷 4.快速答题卡*/
            if (self.examView.testPaperDetails.model!.subjectExamineInfo.type == TFExaminationTypeTestPaper) {
                TalkfunAlertNewView.shareInstance(type: .deleted, target: self, action: #selector(self.understood), ID: result.id)
                
            }else
            {
                TalkfunAlertNewView.shareInstance(type: .questionnaireDeleted, target: self, action: #selector(self.understood), ID: result.id)
               
            }
            //详情
            self.examView.testPaperDetails.closeOrBackUI()
        }
        self.examView.examinationView.examinationDelete(result.id)
      
        let new = TalkfunQuestionnaire()
        new.id = result.id
        self.setCenter.examinationBtn.deleteUI(new)

        if (self.examView.examinationView.dataList.count == 0 ) {
            self.setCenter.examinationBtn.isHidden = true
        }
    }
//    #pragma mark 知道了
    @objc public func understood(btn:UIButton)
    {
         btn.superview?.isHidden = true
         btn.superview?.removeFromSuperview()
    }

//    //网络变更
    @objc public func ReachabilityDidChange(notification:NSNotification)
    {
        let params = notification.userInfo! as NSDictionary
        let Reacha =  params[AFNetworkingReachabilityNotificationStatusItem] as! NSNumber
//           let  ReachabilityStatusUnknown          = -1
           let  ReachabilityStatusNotReachable     = 0
           let  ReachabilityStatusReachableViaWWAN = 1
           let  ReachabilityStatusReachableViaWiFi = 2
        
        let  status  = NSInteger(Reacha.intValue)

        if (status==ReachabilityStatusReachableViaWWAN) {
            //手机网络
            self.isfalseWork = false
            self.cameraView.netImgView.isHidden = true
        }else if (status==ReachabilityStatusReachableViaWiFi)
        {
            //wifi
            self.isfalseWork = false
            self.cameraView.netImgView.isHidden = true
        }else if (status==ReachabilityStatusNotReachable)
        {
            //没有网络
            let  model = TFBitmapModel()
            self.pptView.bitmapView.isHidden = false
            if (self.cameraView.isHidden == false) {
                self.cameraView.netImgView.isHidden = false
            }
            model.imgStr = self.talkfunSDK.roomInfo.zhuboInfo.avatar as NSString

            model.isNOWork = true
            self.isfalseWork = true
            model.isFull = self.isOrientationLandscape
            model.startTime = self.startTime as NSString
            model.isTimeStart = false
            self.pptView.bitmapView.updateLiveStatus1(model)
        }
      
    }
    @objc public func SDKError(notification:Notification) {
        let userInfo:[String:Any] = notification.userInfo as! [String:Any]
        let str:String = userInfo["msg"] as! String
        
        if str.isEmpty {
            
        } else {
            self.TF_alertController(str)
        
        }

     }

//    #pragma mark - 监听事件
    public func registerEventListener()
    {
        //发生错误
        NotificationCenter.default.addObserver(self, selector: #selector(SDKError(notification:)), name: Notification.Name(NSNotification.Name.TalkfunError.rawValue), object: nil)
        
        
        
       let networkReachabilityManager =     AFNetworkReachabilityManager.shared()
        
       networkReachabilityManager.startMonitoring()
        //网络改变
        NotificationCenter.default.addObserver(self, selector: #selector(self.ReachabilityDidChange), name: Notification.Name(NSNotification.Name.AFNetworkingReachabilityDidChange.rawValue), object: nil)
        
        //TODO:监听手机方向
        NotificationCenter.default.addObserver(self, selector: #selector(self.deviceOrientationChange), name: Notification.Name(UIDevice.orientationDidChangeNotification.rawValue), object: nil)
        
        //键盘隐藏前
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide1), name: Notification.Name("LarGeLivekeyboardWillHide"), object: nil)
        
    }
    
    @objc public func keyboardWillHide1()
     {
         self.pptViewTouchesBeganUI()
         print("self.pptView.pptsFunctionView.isHidden===>%i",self.pptView.pptsFunctionView.isHidden)
     }
    //自动换方向
    @objc public func deviceOrientationChange()
    {
       
        if self.navigationController != nil {
            let deviceOrientation = UIApplication.shared.statusBarOrientation
            //本来就是竖屏不再再设置竖屏
            if self.isOrientationLandscape==false  {
                
                if deviceOrientation == .portrait || deviceOrientation == .portraitUpsideDown {
                    
                    return
                }
                if deviceOrientation == .landscapeLeft {
                    self.portrait()
                    return
                }
                
            }
            
            //本来就是横屏不再再设置横屏
            if (self.isOrientationLandscape){
                if (deviceOrientation == .landscapeLeft || deviceOrientation == .landscapeRight) {
                    return
                }
               
            }
            self.view.endEditing(true)

            self.fullScreenUI()

        }
      
    }
    
//    #pragma mark -- 调换ppt容器和摄像头容器
    @objc public func  exchangePPTClick(btn:UIButton)
    {
        if (self.cameraView.isHidden) {
            return
        }
        self.talkfunSDK.exchangePPTAndCameraContainer()
    }

//    #pragma mark -- 摄像头放大
    @objc public func  videofullScreenBtnClick()
    {
        self.isvideofull = true
        self.talkfunSDK.exchangePPTAndCameraContainer()
        self.cameraView.isHidden = true
        self.fullScreen()
    }
//    #pragma mark - 强制竖屏
    public func orientationPortrait()
    {

        self.chatTFView.isHidden = false
    

        self.orientationPortrait(portrait: true)
        self.updateChrysanthemum()
      
    }

    public func system_orientationLandscape()
    {
        self.chatTFView.isHidden = true
        self.isOrientationLandscape = true
        self.orientationPortrait(portrait: false)
        self.updateChrysanthemum()
    }
    public func system_orientationPortrait()
    {
        self.isOrientationLandscape = false
        self.chatTFView.isHidden = false
        self.orientationPortrait(portrait: true)
        self.updateChrysanthemum()

    }
    public func getMaskLayer(_ rect:CGRect) ->CAShapeLayer
    {
        
        var size = CGSize(width: 10.0, height: 10.0)
        if (self.isOrientationLandscape) {
            size = CGSize(width: 0, height: 0)
        }
        let maskPath = UIBezierPath.init(roundedRect: rect, byRoundingCorners:[UIRectCorner.topRight,UIRectCorner.topLeft], cornerRadii: size)
      
        let maskLayer = CAShapeLayer()
        maskLayer.frame = rect
        maskLayer.path = maskPath.cgPath

        return maskLayer
    }
    //MARK:触发公告
    @objc public func announcebtnClick()
    {
    if (self.isOrientationLandscape) {
        self.showHorMoreView(show:false)
    } else {
        self.showMoreview(show: false,animation:true)
      
    }
    self.maskView.isHidden = true
    self.announcementView!.alpha = 1.0
    self.superView.addSubview(self.announcementView!)
  }
    public func getRandomNumber()->CGFloat
      {
          return 60+CGFloat(arc4random_uniform(UInt32(180 - 60)))
      }
    
   static public func statusBarView()->UIView
    {
        if #available(iOS 13.0, *) {

            let keyWindow = TalkfunSharedTools.keyWindow()

            let statusBarView = UIView()
            statusBarView.frame = (keyWindow.windowScene?.statusBarManager!.statusBarFrame)!


            statusBarView.isUserInteractionEnabled = false
  
        
            
            keyWindow.addSubview(statusBarView)
            
            return statusBarView
       } else {
           
           let statusWindow = UIApplication.shared.value(forKey: "statusBarWindow") as! UIView;
           let statusBarView = (statusWindow.value(forKey:"statusBar") as! UIView)
           return statusBarView
           
       }
    }

//    //弹起点名窗口
    @objc public func rollCallClick()
    {
       self.superView.addSubview(self.signInView)
        UIView.animate(withDuration: 0.5) {
     
            self.signInView.view.frame = CGRect(x: 0, y: self.signInView.frame.size.height - self.signInView.view.frame.height, width: self.signInView.frame.size.width, height: self.signInView.view.frame.height)

        }
    }
  //收起更多视图
    @objc public func  tapClick()
     {
         self.view.endEditing(true)
         self.horChatView.biaoqingBtn.isSelected = false
         self.showMoreview(show: false,animation:true)
         self.showEmotionview(show: false)
         if (self.chatTFView.expressionButton.isSelected) {
            self.chatTFView.expressionButton.isSelected = false
            self.chatTFView.takeChatandAsk()
        }
   }
    @objc public func maskViewClickTap()
    {
        if (self.voteSelectVC.isShow) {
            self.showVoteSelect(false)
               
            if (self.votelistVC!.isShow==false) {  //防止投票结束之后,直接点击进入投票详情,点击蒙版不隐藏
                self.maskView.isHidden = true
            }
                    return
                }
        
        if ((self.urlAlertView) != nil) {
            self.urlAlertView!.removeFromSuperview()
            self.urlAlertView = nil
        }

        if (self.votelistVC!.isShow) {
            self.showVoteList(false)
        }


        if (self.networkSelectView.isShow) {
            self.showNetworkSelectview(false)
            return
        }

        if (self.horQuestionView.isShow) {
            self.showHorQuestionRecord(false)
           
            return
        }

        if (self.isOrientationLandscape) {
            self.showHorMoreView(show: false)
         
        } else {
            self.showMoreview(show: false,animation:true)
      
        }
        if ((self.voteSuccessView) != nil) {
            self.closeVoteSuccess()
         
        }
        self.maskView.isHidden = true
    }

   @objc public func alertUrlCommitUI()
    {
        TalkfunSharedTools.openUrl(str: self.urlAlertView!.urlLab.text! as NSString)
    }

    @objc public func url_closeBtnClick()
    {
        self.urlAlertView!.removeFromSuperview()
        self.urlAlertView = nil
        self.maskView.isHidden = true
    }
//    #pragma mark - 选中投票cell , 获取详情
    public func voteDidSelectRowAtIndexPath(_ model:TFVoteData)
    {
        self.view.addSubview(self.voteSelectVC)
        self.talkfunSDK.getVotingDetails(model.vid) { code, model, error in
            
            if (code == 0 ) {

               
                self.voteSelectVC.updataVote(model!)

                self.showVoteSelect(true)
            }else{
                self.TF_toast( error!.msg )
            }
        }
    }
//    #pragma mark - 提交投票
    @objc public func submitVote()
    {
        let model = TFSubmit()
        for i in 0..<self.voteSelectVC.result.opList.count {
            let model1111 = self.voteSelectVC.result.opList[i] as! TFOp
            if (model1111.selected) {
                model.option.add(NSString.init(format: "%o", i))

            }
        }
        model.vid = self.voteSelectVC.result.vid

        self.talkfunSDK.voteSubmit(model) {[weak self] code, error in
            guard let `self` = self else { return }
            if (code == 0) {
                self.closeVoteSelect()
                self.closeVoteList()
              
                self.maskView.isHidden = false
                self.view.addSubview(self.voteSuccessView!)
                self.startVoteSuccessTimers()
           
            } else {
                self.TF_toast( error!.msg )
             
            }
        }
    }

//    //开启投票成功倒计时
  @objc  public func startVoteSuccessTimers()
    {
        if self.voteSuccesstimer != nil {
            self.voteSuccesstimer?.invalidate()
            self.voteSuccesstimer = nil
        }
        self.voteSuccesstimer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(closeVoteSuccess), userInfo: nil, repeats: true)
        RunLoop.main.add(self.voteSuccesstimer! , forMode: RunLoop.Mode.default)
      
    }
//    #pragma mark 投票开始的事件回调
    public func onVoteNew(_ result: TFVoteData) {
        self.setCenter.voteButton.isHidden = false
        self.setCenter.layoutSubviews()
        self.view.addSubview(self.voteSelectVC)
        self.voteSelectVC.updataVote(result)
        self.showVoteSelect(true)
    }

//    #pragma mark 投票结束的事件回调
    public func  onVotePub(_ result: TFVoteData) {
        self.chatContrller.update_Vote(model: result)
        self.horChatController!.update_Vote(model: result)
        
    }
    //请求投票列表
    public func getVotingList()
    {
        if(self.talkfunSDK.roomInfo.liveStatus == TalkfunLiveStatusStart)
        {
        self.talkfunSDK.getVotingList {[weak self] code, list, error in
            guard let `self` = self else { return }
            
            if (code == 0) {
                var isVote:Bool = false
                for i in 0..<list!.count {
                    let data =  list![i] as! TFVoteData
                    let endTime = data.info.endTime as NSString
                    if (endTime == "0" && data.voted==false) {
                        isVote = true
                        
                    }
                }
                if (list!.count == 0) {
                    self.setCenter.voteButton.isHidden = true
                } else {
                    self.setCenter.voteButton.isHidden = false
                }
                self.setCenter.layoutSubviews()
                if (isVote) {
                    self.setCenter.voteButton.setTitle("投票中", for: .normal)
                    
                } else {
                    self.setCenter.voteButton.setTitle("投票", for: .normal)
                    
                }
                self.votelistVC!.updataVote(list!)
            } else {
                self.TF_toast( error!.msg )
                
            }
        }
    }
    }
//    #pragma mark - 获取投票列表
    @objc public func voteClick()
    {
        self.view.addSubview(self.votelistVC!)
        self.showVoteList(true)
        self.getVotingList()
    }
    @objc public func networkSelectClose()
     {
         self.showNetworkSelectview(false)
     }
//    #pragma mark - scrollView 代理方法
    public func  scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        self.view.endEditing(true)
    }
    public func  scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        
        
        //聊天与提问都为关
        if (self.talkfunSDK.roomInfo.mod_text_live.config.chat.enable == false && self.talkfunSDK.roomInfo.mod_text_live.config.qa.enable == false ) {
            self.scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
        } else if (self.talkfunSDK.roomInfo.mod_text_live.config.chat.enable == true && self.talkfunSDK.roomInfo.mod_text_live.config.qa.enable == false) {
            //隐藏提问
            self.scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
        }else {
            //聊天与提问都开启  or //隐藏聊天
            
            self.chatTFView.textView.text = ""
            self.chatTFView.textView.resignFirstResponder()
            if self.scrollView.contentOffset.x>0 && self.scrollView.frame.size.width>0 {
                let num = NSInteger(self.scrollView.contentOffset.x/self.scrollView.frame.size.width)
                    if (num == 1) {
             
                         self.buttonView.selectButton(self.buttonView.askBtn)
                         self.chatTFView.isChat = false
                         self.chatTFView.textView.text = ""
                         self.chatTFView.takeChatandAsk()
                        //收起更多视图
                         self.tapClick()
                     }
            }else
            {
                self.buttonView.selectButton(self.buttonView.chatBtn)
                self.chatTFView.isChat = true
                //收起更多视图
                self.chatTFView.takeChatandAsk()
            }
        
        }
        
        
        
        
        
       
    }
    public func showVoteList(_ isShow:Bool)
    {
        self.votelistVC!.isShow = isShow
        
        var duration:CGFloat = 0.25
        if (self.isOrientationLandscape) {
            duration = (self.superView.frame.width - 375.0) / self.superView.frame.width/2.0
        }else
        {
            duration = (self.superView.frame.height - 462.0) / self.superView.frame.height/2.0
        }
        UIView.animate(withDuration: duration) {
            self.votelistVC!.isShow = isShow
           // //横屏
            if (self.isOrientationLandscape) {

                if (isShow) {
                    self.votelistVC!.frame = CGRect(x: self.superView.frame.width-375, y: 0, width: 375.0, height: self.superView.frame.height)
             
                    self.maskView.isHidden = false

                    
                    let masklayer = self.getMaskLayer(self.votelistVC!.bounds)
                    self.votelistVC!.layer.mask = masklayer
                } else {
                    self.votelistVC!.frame = CGRect(x: self.superView.frame.width, y: 0, width: 375.0, height: self.superView.frame.height)
                }


            } else {

                if (isShow) {
                    self.votelistVC!.frame = CGRect(x: 0, y: self.superView.frame.height-462.0, width: self.superView.frame.size.width, height: 462.0)
               
                    self.maskView.isHidden = false

                    let masklayer = self.getMaskLayer(self.votelistVC!.bounds)
                    self.votelistVC!.layer.mask = masklayer
                } else {
                    self.votelistVC!.frame = CGRect(x: 0, y: self.superView.frame.height, width: self.superView.frame.width, height: 462.0)
                    
                }

            }

        }
    }
    public func showVoteSelect(_ isShow:Bool)
    {
        var duration = 0.25
              if (self.isOrientationLandscape) {
                  duration = (self.superView.frame.width-375.0)/self.superView.frame.width / 2.0
              }else
              {
                  duration = (self.superView.frame.height-462.0)/self.superView.frame.height/2.0
              }
        UIView.animate(withDuration: duration) {
            self.voteSelectVC.isShow = isShow
            if (self.isOrientationLandscape) {  //Yes为横屏
                if (isShow) {

                    self.voteSelectVC.frame = CGRect(x: self.superView.frame.width-375.0, y: 0, width: 375.0, height: self.superView.frame.height)

                    self.maskView.isHidden = false
                    self.voteSelectVC.layer.mask = self.getMaskLayer(self.voteSelectVC.bounds)
                    
                } else {
                    self.voteSelectVC.frame = CGRect(x: self.superView.frame.width, y: 0, width: 375.0, height: self.superView.frame.height)
                    self.voteSelectVC.backButtonClickUI()
                 
                }
            } else { //false为竖屏
                if (isShow) {

                    self.voteSelectVC.frame = CGRect(x: 0, y: self.superView.frame.height-462.0, width: self.superView.frame.width, height: 462.0)
                  
                    self.maskView.isHidden = false
                    self.voteSelectVC.layer.mask = self.getMaskLayer(self.voteSelectVC.bounds)
                } else {
                    self.voteSelectVC.frame = CGRect(x: 0, y: self.superView.frame.height, width: self.superView.frame.width, height: 462.0)
                  
                    self.voteSelectVC.backButtonClickUI()
                }
            }
        }
              
    }
    @objc public func closeVoteSuccess()
      {
          self.voteSuccessView!.removeFromSuperview()
          self.voteSuccessView = nil
          if self.voteSuccesstimer != nil {
              self.voteSuccesstimer!.invalidate()
              self.voteSuccesstimer = nil
              self.maskView.isHidden = true
          }
       
      }
      @objc public func closeVoteSelect()
      {
          self.showVoteSelect(false)
          self.maskView.isHidden = true
      }
      @objc public func showVoteSelect()
      {
          self.showVoteSelect(false)
      }
    //关闭投票列表
    @objc public func closeVoteList()
      {
      self.showVoteList(false)
      self.maskView.isHidden = true
      }

    @objc public func btnViewButtonsClicked(_ btn:UIButton)
    {
            self.buttonView.selectButton(btn)
            if (btn == self.buttonView.askBtn)
        {
                self.scrollView.setContentOffset(CGPoint(x: self.scrollView.frame.size.width, y: 0), animated: false)
              
                self.chatTFView.isChat = false
    
                self.chatTFView.textView.text = ""
    
                self.chatTFView.takeChatandAsk()
                self.tapClick()
            }else {
        
                self.scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
            self.chatTFView.isChat = true
            self.chatTFView.textView.text = ""
            self.chatTFView.takeChatandAsk()
   
            }
                                                 
     }
     //表情view是否显示
    public func showEmotionview(show:Bool)
    {
        self.view.addSubview(self.emotionView)
        UIView.animate(withDuration: 0.25) {
        if (self.isOrientationLandscape) { //横屏
    
            if (show) {
              
                self.emotionView.frame = CGRect(x: 0, y: self.superView.frame.height-self.ExpressionViewHeight(), width: self.superView.frame.width, height: self.ExpressionViewHeight())
                        
                  
                self.emotionView.collection.reloadData()

                        
                self.horChatView.frame = CGRect(x: self.horChatView.frame.origin.x, y:  self.emotionView.frame.origin.y-self.horChatView.frame.height, width: self.horChatView.frame.width, height: self.horChatView.frame.height)
    
                    } else {
    
                        self.emotionView.frame = CGRect(x: 0, y: self.superView.frame.height, width: self.superView.frame.width, height: self.ExpressionViewHeight())
      
                        if self.horChatView.textView.isFirstResponder {
                            
                        }else
                        {
                            let horChatViewY = self.getCalculateViewFrame(self.isOrientationLandscape).height-self.horChatView.frame.height-self.isOrientationiPhoneX
                            
                            self.horChatView.frame = CGRect(x: self.horChatView.frame.origin.x, y:  horChatViewY, width: self.horChatView.frame.width, height: self.horChatView.frame.height)
                        }
                        
                    }
    
                    self.horChatController!.frame = CGRect(x: 0, y: self.superView.frame.height-21-192-self.isOrientationiPhoneX, width: 320, height: 192)
            
                } else {
                    if (show) {
                        self.emotionView.frame = CGRect(x: 0, y: self.superView.frame.height-self.ExpressionViewHeight(), width:  self.superView.frame.width, height: self.ExpressionViewHeight())
                        
                    
                        self.emotionView.collection.reloadData()
    
                        self.chatTFView.expresssionClick = true
                        
                        let chatTFViewY:CGFloat = self.emotionView.frame.origin.y-self.chatTFView.frame.height
                     
                        
                        self.chatTFView.frame = CGRect(x: self.chatTFView.frame.origin.x, y:chatTFViewY  , width: self.chatTFView.frame.width, height: self.chatTFView.frame.height)
                    } else {
    
                        self.emotionView.frame = CGRect(x: 0, y: self.superView.frame.height, width: self.superView.frame.width, height: self.ExpressionViewHeight())
                        

                        self.chatTFView.expresssionClick = false
                        
                        self.moreView!.frame = CGRect(x: 0, y:self.getCalculateViewFrame(self.isOrientationLandscape).height, width: self.superView.frame.width, height: 130)
                        
                        if self.chatTFView.textView.isFirstResponder {
                            
                        }else
                        {
                            self.chatTFView_frame()
                        }
                            

                    }
    
                }
      
        }
    }
    
    public func chatTFView_frame()
    {
        var chatTFViewY:CGFloat = self.getCalculateViewFrame(self.isOrientationLandscape).height-self.chatTFView.frame.height-self.chatTFView.keyboardFrameEnd.height-self.isOrientationiPhoneX
        
        //已经显示更多选项
        if self.moreView!.frame.origin.y != self.getCalculateViewFrame(self.isOrientationLandscape).height {
            chatTFViewY = chatTFViewY - self.moreView!.frame.height + self.isOrientationiPhoneX
        }
        
        self.chatTFView.frame = CGRect(x: self.chatTFView.frame.origin.x, y: chatTFViewY , width: self.chatTFView.frame.width, height: self.chatTFView.frame.height)
    }
    @objc public func  askBtnClick(btn:UIButton)
    {
        btn.isSelected = !btn.isSelected
           self.chatTFView.textView.text = ""
           if (self.chatTFView.expressionButton.isSelected) {
               self.chatTFView.expressionButton.isSelected = false
               self.showEmotionview(show: false)
              
        }
        self.chatTFView.takeChatandAsk()
            
    }
    // 更多
    @objc public func  moreBtnClicked(btn:UIButton)
    {
        btn.isSelected = !btn.isSelected
       if (btn.isSelected) {
        
           //显示公告
           self.showMoreview(show: true,animation:true)
     
       } else {
       
           // 顺序不能改
           self.showMoreview(show: false,animation:true)
           
       }
    }
    //表情触发
    @objc public func expressionBtnClick(btn:UIButton)
    {
    btn.isSelected = !btn.isSelected
    if (btn.isSelected) {
        //表情选项
        self.view.endEditing(true)
       
        self.showEmotionview(show: true)
      
     
    } else {
      // 文字聊天
        self.showEmotionview(show: false)
        self.showMoreview(show:false,animation:true)
    }
        self.chatTFView.takeChatandAsk()
}
  //MARK:触发网络选择
    @objc public func networkSelectbtnClick()
    {
        self.showNetworkSelectview(true)
    }
    //#pragma mark 直播间模式切换
    public func  onLiveModeChange(_ status: TFLiveModeChange) {
        if (status.currentMode == TalkfunLiveModeDesktop) {
          
            
        }else{
         
            
        }
    }
    //MARK: ---------------- 进入房间的事件回调
    public func  onEnterRoom(_ code: Int, result error: TFError) {

        if ( code == 0 ) {
//            print(self.talkfunSDK.roomInfo.liveMode)
            
            if (self.talkfunSDK.roomInfo.visitors) {
                self.onlineLabel.isHidden = false
            }else{
                self.onlineLabel.isHidden = true
            }
            
            //考试开关
            self.configExaminationUI()
  
            
            self.scrollView.isHidden = false
            self.buttonView.chatBtn.isHidden = false
            self.buttonView.askBtn.isHidden = false
            self.buttonView.chatBtn.isUserInteractionEnabled = true
            self.buttonView.askBtn.isUserInteractionEnabled = true
           
            self.scrollView.isScrollEnabled = true
            self.chatTFView.askButton.isUserInteractionEnabled = true
            self.horChatView.askAndChat_Btn.isUserInteractionEnabled = true
            
            // //默认是不开启互动区域
            var chatAreaStatus:TFChatAreaStatus = .AllNone
            //聊天与提问都为关
            if (self.talkfunSDK.roomInfo.mod_text_live.config.chat.enable == false && self.talkfunSDK.roomInfo.mod_text_live.config.qa.enable == false ) {
                chatAreaStatus = .AllNone
                
                self.buttonView.askBtn.isHidden = true
                self.buttonView.askBtn.isUserInteractionEnabled = false
                self.buttonView.chatBtn.isUserInteractionEnabled = false
            } else if (self.talkfunSDK.roomInfo.mod_text_live.config.chat.enable == false && self.talkfunSDK.roomInfo.mod_text_live.config.qa.enable == true) {
                //隐藏聊天
                chatAreaStatus = .Ask
               
            } else if (self.talkfunSDK.roomInfo.mod_text_live.config.chat.enable == true && self.talkfunSDK.roomInfo.mod_text_live.config.qa.enable == false) {
                //隐藏提问
                chatAreaStatus = .Chat
                self.buttonView.askBtn.isHidden = true
                self.buttonView.askBtn.isUserInteractionEnabled = false
                self.buttonView.chatBtn.isUserInteractionEnabled = false
            }else if (self.talkfunSDK.roomInfo.mod_text_live.config.chat.enable == true && self.talkfunSDK.roomInfo.mod_text_live.config.qa.enable == true) {
                //聊天与提问都开启
               chatAreaStatus = .AskAndChat
            }
            self.buttonView.layoutSubviews()
            self.scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
            
            self.chatTFView.status = chatAreaStatus
            self.horChatView.status = chatAreaStatus
            
             //课件下载开关
            if (self.talkfunSDK.roomInfo.mod_document_download_live.enable) {
                self.courseDownWithIsShow(true)
             
            } else {
                self.courseDownWithIsShow(false)
            }

            self.chatTFView.takeChatandAsk()
            
            self.horChatView.takeChatandAsk()
            ////请求投票列表
            self.getVotingList()
            
        }else{

            self.TF_alertController(error.msg)
        }

    }
    
//    #pragma mark 全体解禁言
    public func  onChatDisableAll(_ state: Bool) {
        let  model = TFDisableChatModel()
        model.isAll = true
        model.disableAllChat = state
        self.isAbleChat = state
        self.chatContrller.update_disabelChat(model: model)
        self.horChatController!.update_disabelChat(model: model)
        self.chatTFView.isAbleChat = state
        self.horChatView.isAbleChat = state

        self.chatTFView.takeChatandAsk()
        self.horChatView.takeChatandAsk()
        self.chatTFView.disabelChat.text = "全体禁言中"
        self.horChatView.disabelChat.text = "全体禁言中"
    }
//    #pragma mark 单体禁言 单体解除禁言  true 禁止聊天 false为允许
    public func  onChatDisable(_ state: Bool) {

        let model = TFDisableChatModel()
        model.isAll = false
        model.disableChat = state
        self.isAbleChat = state
        self.chatContrller.update_disabelChat(model: model)
        self.horChatController!.update_disabelChat(model: model)
        self.horChatView.isAbleChat = state
        self.chatTFView.isAbleChat = state
        self.chatTFView.takeChatandAsk()
        self.horChatView.takeChatandAsk()
        self.chatTFView.disabelChat.text = "禁言中"
        self.horChatView.disabelChat.text = "禁言中"
    }
//    #pragma mark 广播
    public func onBroadcast(_ result: TFBroadcast) {
        self.chatContrller.updata_Notice(model: result)
        self.horChatController!.updata_Notice(model: result)
    }
//    #pragma mark 网络速度 网络状态
    public func  onNetworkSpeed(_ result: TFNetworkSpeed) {

        self.networkStatus = result.networkStatus
        if (result.networkStatus == TalkfunNetworkStatusBad) {

            if (self.pptView.pptsFunctionView.isShowOnce == false) {
                self.pptView.pptsFunctionView.stratSpeedUI()
           
            }
        }
    }
//   #pragma mark  桌面分享开始
    public func  liveDesktopStart()
    {
        self.isDesktop = true
        self.cameraView.removeFromSuperview()
    }
//    #pragma mark 桌面分享结束
    public func  liveDesktopStop()
    {
        self.isDesktop = false
        self.view.addSubview(self.cameraView)
        //视频在聊天输入下面
        self.view.addSubview(self.chatTFView)
        self.view.addSubview(self.horChatView)
        
    }
// #pragma mark 摄像头开启
    public func  onCameraShow()
    {
         self.cameraShow = true
        self.pptView.bitmapView.isHidden = true
        if (self.talkfunSDK.isLoadCourseware) {  //是否载入课件
            self.cameraView.isHidden = false
            self.horChatView.chuangkou_Btn.isHidden = false
        } else {
            if(self.talkfunSDK.isExchanged==false)
            {
                self.talkfunSDK.exchangePPTAndCameraContainer()
                self.cameraView.isHidden = true
            }
 
            self.horChatView.chuangkou_Btn.isHidden = true

        }
        if (self.isOrientationLandscape) {
            self.horChatView.takeChatandAsk()
        }

    }
    //课件下载按键隐藏与显示
    public func courseDownWithIsShow(_ isShow:Bool)
    {
        self.moreView!.courseDown(isShow)
        self.horMoreView.courseDown(isShow)
    }
//    #pragma mark  摄像头关闭
    public func onCameraHide()
    {
        self.cameraShow = false
        self.cameraView.isHidden = true
        self.horChatView.chuangkou_Btn.isHidden = true
        if (self.talkfunSDK.isLoadCourseware == false) {
            self.pptView.bitmapView.isHidden = false
            let model = TFBitmapModel()
            model.status = self.talkfunSDK.liveStatus
            model.isFull = self.isOrientationLandscape
            model.imgStr = self.talkfunSDK.roomInfo.zhuboInfo.avatar as NSString
            model.startTime = NSString.init(format: "%ld", self.talkfunSDK.roomInfo.startTime)
            
            if (TalkfunSharedTools.getCurrentDateString() > self.talkfunSDK.roomInfo.startTime ) {
            model.isTimeStart = false
            }else{
            model.isTimeStart = true
            }
            self.pptView.bitmapView.updateLiveStatus1(model)
          

        } else {
            self.pptView.bitmapView.isHidden = true
        }

        if (self.isOrientationLandscape) {
            self.horChatView.takeChatandAsk()
      
        }

    }
//    #pragma mark ----- 接收滚动通知事件回调--------------
    public func onAnnounceRoll(_ result: TFAnnounceRoll) {
        self.hornView!.announceRoll(obj: result)
        self.superView.addSubview(self.hornView!)
    }

//    #pragma mark  载入课件
    public func loadCourseware()
    {
        self.pptView.bitmapView.isHidden = true

            if (self.cameraShow) {
                self.talkfunSDK.exchangePPTAndCameraContainer()
      
                self.cameraView.isHidden = false
                self.horChatView.chuangkou_Btn.isHidden = false
            } else {
                self.cameraView.isHidden = true
                self.horChatView.chuangkou_Btn.isHidden = true
            }

        if (self.isOrientationLandscape) {
            self.horChatView.takeChatandAsk()

        }
    }
//    #pragma mark 接收主播发起签到的事件回调
    public func onSignNew(_ result: TFSignNew) {
        self.signInView.frame = CGRect(x: (self.superView.frame.width-240)/2, y: (self.superView.frame.height-312)/2, width: 240, height: 312)
        self.superView.addSubview(self.signInView)

        self.signInView.onSignNewUI(result: result)
        self.setCenter.lotteryCall.isHidden = true
        self.setCenter.rollCall.isHidden = false
        self.setCenter.layoutSubviews()
    }
//    #pragma mark - 结束签到的事件回调
    public func onSignEnd(_ result: TFSignEnd) {
        
        self.signInView.onSignEndUI(result: result)
        self.TF_toast( "点名结束")
        self.setCenter.rollCall.isHidden = true
        self.setCenter.layoutSubviews()
    }
//    #pragma mark 推送公告的事件回调
    public func onAnnouncement(_ result: TFAnnouncement) {
        if(self.talkfunSDK.roomInfo.liveStatus == TalkfunLiveStatusStart)
        {
            
      
        if self.horMoreView.isHidden == false {
            self.horMoreView.showView(false)
        }
       
          
        self.announcementView!.alpha = 1
        self.announcementView!.setAnnouncement(result)
        self.superView.addSubview(self.announcementView!)
        }
    }
    
    public func networkSelectBtnHidden(_ hidde:Bool)
    {
        self.moreView!.networkSelectBtn.isHidden = hidde
        self.horMoreView.netWorkBtn.isHidden = hidde
        
        self.moreView!.layoutSubviews()
    }
    
    public func getLineListUI()
    {
        self.talkfunSDK.getLineList {[weak self] result in
            guard let `self` = self else { return }

            if (result!.code == 0) {
              
                self.networkSelectBtnHidden(false)

           if (self.talkfunSDK.roomInfo.mod_document_download_live.enable) {
               self.courseDownWithIsShow(true)
            }
            self.networkSelectView.updataNetWork(model: result!)
        } else {
            self.networkSelectBtnHidden(true)
            self.TF_toast( result!.msg )
            
        }

        }
    }
    
//    #pragma mark 直播间状态
    public func onLiveStatus(_ status: TalkfunLiveStatus) {

        let model = TFBitmapModel()

        if (status == TalkfunLiveStatusWait) {

            self.networkSelectBtnHidden(true)
        

            //  当前时间 大于   开始时间课程
            if (TalkfunSharedTools.getCurrentDateString() > self.talkfunSDK.roomInfo.startTime ) {

            //主播暂时不在
            model.isTimeStart = false
        }else{
            ////日期内容

            model.isTimeStart = true
        }

        }
        else if (status == TalkfunLiveStatusStart) {

//            self.pptView.bitmapView.isHidden = false
            self.addWatermarkLayer()
            
            TalkfunDownloadManager.share().pauseAllDownload()
            
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+2) {
                self.getLineListUI()
                   }
           

        }
        else if (status == TalkfunLiveStatusStop) {
            self.preventCheatCountdown.remind.isShow = false
            self.preventCheatCountdown.remind.view.isHidden = true
            self.preventCheatCountdown.remind.successView.isHidden = true
//----------
            self.pptView.bitmapView.isHidden = false

            self.cameraView.isHidden = true
          
            self.networkSelectBtnHidden(true)
            //大班主播主动结束点名, 弹窗不消失处理
            self.signInView.onSignEndUI(result: TFSignEnd())
            
            if self.preventCheatCountdown.remind.countdownTime != nil {
                 self.preventCheatCountdown.remind.countdownTime?.invalidate()
                 self.preventCheatCountdown.remind.countdownTime = nil
             }
           
        }

        model.status = status
       
        model.startTime = NSString.init(format: "%ld", self.talkfunSDK.roomInfo.startTime)
        model.imgStr = self.talkfunSDK.roomInfo.zhuboInfo.avatar as NSString
        model.isFull = self.isOrientationLandscape
        self.pptView.bitmapView.updateLiveStatus1(model)
    }
//    #pragma mark  桌面分享
    public func isRealDesktop(_ isDesktop: Bool) {
            
        if (self.talkfunSDK.isLoadCourseware == false) {
        
             self.pptView.bitmapView.isHidden = isDesktop
         }
     
    }
//    #pragma mark 实时课件下载开关状态
    public func onDocumentDownload(_ enable: Bool) {
        self.courseDownWithIsShow(enable)
    }
//    #pragma mark 相互挤下线
    public func onMemberForceout()
    {
        self.talkfunSDK.pause()
        let alert:UIAlertController = UIAlertController.init(title: "温馨提示", message: "你被迫下线了", preferredStyle: .alert)
        let otherAction:UIAlertAction = UIAlertAction.init(title: "退出", style: .default) { action in
            self.quit()
        }
        let refreshAction:UIAlertAction = UIAlertAction.init(title: "刷新", style: .default) { action in
            self.talkfunSDK.reload()
        }
        alert.addAction(otherAction)
        alert.addAction(refreshAction)
        self.present(alert, animated: true, completion: nil)
    }
//    #pragma mark 当前用户被踢出房间的事件回调
    public func onMemberKick()
    {
        let alert:UIAlertController = UIAlertController.init(title: "温馨提示", message: "你被踢出房间了", preferredStyle: .alert)
        let otherAction:UIAlertAction = UIAlertAction.init(title: "确认", style: .default) { action in
            self.quit()
        }
        alert.addAction(otherAction)
        self.present(alert, animated: true, completion: nil)
    }
//    #pragma mark 接收别人的聊天事件回调
    public func onDidReceiveText(_ result: TFChatData) {
        self.addChatData(chatData: result)
        self.loadData(msg: result.msg as NSString)
    }
    //删了聊天数据
    public func onChatDel(_ uuid: String) {
        
        self.chatContrller.ChatDel(uuid: uuid)
        self.horChatController!.ChatDel(uuid: uuid)
        
    }
    //抽奖入口
  @objc public func lotteryClick()
    {
        //删除中奖人
        self.myLotteryVC!.view.removeFromSuperview()
        self.myLotteryVC = nil
        //删除中奖人列表
        self.listLotteryVC!.view.removeFromSuperview()
        self.listLotteryVC = nil
        self.view.endEditing(true)
        self.lotteryVC!.view.alpha = 1.0
        self.superView.addSubview(self.lotteryVC!.view)
        self.lotteryVC!.lotteryStartUI()
    }
//    #pragma mark 抽奖开始
    public func onLotteryStart(_ result: TFLotteryStart) {
        self.setCenter.lotteryCall.isHidden = false
        self.setCenter.layoutSubviews()
       
        //删除中奖人
        self.myLotteryVC!.view.removeFromSuperview()
        self.myLotteryVC = nil
        //删除中奖人列表
        self.listLotteryVC!.view.removeFromSuperview()
        self.listLotteryVC = nil
        
        self.view.endEditing(true)

        self.lotteryVC!.view.alpha = 1.0
        self.view.addSubview(self.lotteryVC!.view)
       
        self.lotteryVC!.lotteryStartUI()
    }

     //TODO:  同时在线人数  (真实人+虚拟人员 的总数)

    public func onPeakConcurrentUsers(_ total: Int) {
        
        self.onlineLabel.text = String.init(format: "在线 %ld 人", total)
        
        
    }
    //TODO:  抽奖结束
    public func onLotteryStop(_ result: TFLotteryStop) {
        if (self.setCenter.lotteryCall.isHidden) {
            self.setCenter.lotteryCall.isHidden = false
            self.setCenter.layoutSubviews()
        }
        //删除抽奖中的动画
        self.lotteryVC!.view.removeFromSuperview()
        self.lotteryVC = nil
        self.listLotteryVC!.listData.removeAllObjects()
        self.view.endEditing(true)
        ///**奖品名称*/
        self.prize_name = (result.prize_name as NSString) as String
        //中奖人是否有自己
        var  isMe:Bool = false
        for i in 0..<result.winnerList.count {
            let dict = result.winnerList[i] as! TFLotteryWinner

            if (dict.isMe) {
                isMe = true
                break
            }
        }
     
        //聊天有中奖信息
        if(isMe||result.pub)
        {
            var nickname:NSString = ""
            
            self.listLotteryVC!.listData.addObjects(from: result.winnerList as! [Any])

            for i in 0..<result.winnerList.count {

                let nameDict = result.winnerList[i] as! TFLotteryWinner
                var inx:NSString = ""
                if ( i < 10) {
                    inx  = (inx as String) + "0" as NSString
                }
                inx = (inx as String) + (NSString.init(format: "%i.", i+1) as String) as NSString

                inx = "  " + (inx as String) as NSString
                
                let name:NSString = nameDict.nickname as NSString
                
                nickname = (nickname as String) + (inx as String) + (name as String) as NSString
            }

        }
        self.lotteryVC!.view.alpha = 1.0
        self.view.addSubview(self.lotteryVC!.view)
        ///是否中奖
        if (isMe) {
            self.lotteryVC!.iswinner = true
        } else {
            self.lotteryVC!.iswinner = false
        }
        //是否公布
        if (result.pub) {
            self.lotteryVC!.ispub = true
        } else {
            self.lotteryVC!.ispub = false
        }
        self.lotteryVC!.lotteryStopUI()
     
    }
    //TODO: TFLotteryViewControllerDelegate- 转盘结束回调结果
    public func updateLotterView(winner:Bool,pub:Bool)
    {
        self.setCenter.lotteryCall.isHidden = true
        self.setCenter.layoutSubviews()
       
        //删除抽奖开始
        self.lotteryVC!.view.removeFromSuperview()
        self.lotteryVC = nil;
        
        self.view.addSubview(self.myLotteryVC!.view)
        if (winner) {
            
            self.myLotteryVC!.createwinnerView()
            
        } else {
            self.myLotteryVC!.createNOwinnerView()

        }
        let prize_name:NSString = self.prize_name as NSString
        if (prize_name.length > 0) {
            self.myLotteryVC!.winnerLab.text = prize_name as String
            self.myLotteryVC!.winnerIMg.isHidden = false
            self.myLotteryVC!.nowinnerIMg.isHidden = false
        } else {
            self.myLotteryVC!.winnerIMg.isHidden = true
            self.myLotteryVC!.nowinnerIMg.isHidden = true
        }
        self.myLotteryVC!.winnerBtn.isHidden = !pub
        self.myLotteryVC!.nowinnerBtn.isHidden = !pub
    }
    ///TODO:  TFMyLotteryViewControllerDelegate 点击了查询中奖列表
    public func getlottoryList() {
        //删除中奖人
        self.myLotteryVC!.view.removeFromSuperview()
        self.myLotteryVC = nil
        //删除抽奖开始
        self.lotteryVC!.view.removeFromSuperview()
        self.lotteryVC = nil
        
        self.view.addSubview(self.listLotteryVC!.view)
        self.listLotteryVC!.tableview.reloadData()

    }
//    //关闭中奖列表
    @objc public func closelistLotterVC()
    {
        UIView.animate(withDuration: 0.5, delay: 0.0, options: UIView.AnimationOptions.allowAnimatedContent) {
            self.listLotteryVC!.view.alpha = 0.0
        } completion: { Bool in
            
            self.listLotteryVC!.view.removeFromSuperview()
            self.listLotteryVC = nil
            
            self.myLotteryVC!.view.removeFromSuperview()
            self.myLotteryVC = nil
            
        }
    }
    @objc public func  deleteBtnClicked()
    {
        UIView.animate(withDuration: 0.25, delay: 0.0, options: UIView.AnimationOptions.allowAnimatedContent) {
            self.lotteryVC!.view.alpha = 0.0
        } completion: { Bool in
            self.lotteryVC!.view.removeFromSuperview()
            self.lotteryVC = nil
        }
    }
//    #pragma mark  其它人进入房间触发
    public func onMemberJoinOther(_ result: TFMember) {
        self.chatContrller.update_Join(model: result)
        self.horChatController!.update_Join(model: result)
    }
//    #pragma mark 主播回答了问题
    public func onQuestionReply(_ result: TFQuestionData) {
        self.chatContrller.pePly_Question(model: result)
        self.questionContrller.pePly_Question(model: result)
        self.horQuestionView.pePly_Question(model: result)
        self.horChatController!.pePly_Question(model: result)
        if (result.isMe) {
            self.superView.addSubview(self.replyView!)
        }
    }
//    #pragma mark 提问删除
    public func onQuestionDelete(_ result: TFQuestionData) {
        self.chatContrller.remove_Question(model: result)
        self.questionContrller.remove_Question(model: result)
        self.horQuestionView.remove_Question(model: result)
        self.horChatController!.remove_Question(model: result)
    }
//    #pragma mark 接收别人的提问
    public func onQuestionAsk(_ result: TFQuestionData) {
        self.chatContrller.update_Question(model: result)
        self.questionContrller.update_Question(model: result)
        self.horQuestionView.update_Question(model: result)
        self.horChatController!.update_Question(model: result)
    }
//    #pragma mark 提问审核通过
    public func onQuestionAudit(_ result: TFQuestionData) {
        self.chatContrller.update_Question(model: result)
        self.questionContrller.update_Question(model: result)
        self.horQuestionView.update_Question(model: result)
        self.horChatController!.update_Question(model: result)
    }
//    #pragma mark  添加水印
    public func addWatermarkLayer()
    {
        if(self.talkfunSDK.roomInfo.mod_theftproof.enable){
            if (self.layer==nil) {
                let name = self.talkfunSDK.roomInfo.mod_theftproof.msg as NSString

            
                self.layer = CATextLayer.TF_watermark(text: name as String, font: UIFont.PingFang(size: 11), point: CGPoint(x: 0, y: 50))
                self.pptView.layer.addSublayer(self.layer!)
                self.arc4random_uniformFrame()

            }
        }

    }

  //生成layer层
   @objc public func arc4random_uniformFrame()
    {
        if (self.layer != nil) {
        if self.layerTimer != nil {
            self.layerTimer?.invalidate()
            self.layerTimer = nil
        }
        self.layerTimer = Timer.scheduledTimer(timeInterval: self.getRandomNumber(), target: self, selector: #selector(arc4random_uniformFrame), userInfo: nil, repeats: true)
            RunLoop.main.add(self.layerTimer! , forMode: RunLoop.Mode.default)
        self.layer!.frame = self.getLayerFrame()
        }
    }
    //生成位置
   public func getLayerFrame()->CGRect{
       let layerX = self.pptView.frame.size.width - self.layer!.frame.size.width
       let layerY = self.pptView.frame.size.height - self.layer!.frame.size.height
       let rect = CGRect(x: CGFloat(arc4random_uniform(UInt32(layerX))), y: CGFloat(arc4random_uniform(UInt32(layerY))), width: self.layer!.frame.size.width, height: self.layer!.frame.size.height)
    
        return rect
    }

    @objc public func horQuestionRecord(btn:UIButton)
    {
       
        self.showHorQuestionRecord(true)
    }
    public func showHorQuestionRecord(_ isShow:Bool)
    {
        self.horQuestionView.isShow = isShow
        self.horQuestionView.showView(isShow)
    }

    public func showHorMoreView(show:Bool)
    {
        self.horMoreView.showView(show)
    }
//    #pragma mark   横屏提问记录
    @objc public func closeQuestionRecord()
    {
        self.showHorQuestionRecord(false)
    }
    // 点击超链接
    public func HorChat_clickToUrl(_ url: NSString) {
        if (url.length > 4) {
            self.urlAlertView!.urlLab.text = url as String
            self.superView.addSubview(self.urlAlertView!)
        self.maskView.isHidden = false
        
        }
    }
    //投票结果
    public func HorVote_clickToDetail(_ model: TFVoteData) {
        self.voteDidSelectRowAtIndexPath(model)
    }
   @objc public func roundClick()
    {
        self.talkfunSDK.sign(in: self.signInView.result!.signId) {[weak self] code, error in
            guard let `self` = self else { return }
 
            if (code == 0 ) {
                
                self.setCenter.rollCall.isHidden = true
                self.setCenter.layoutSubviews()
                self.signInView.roundClickUI()
          
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+2) {
                    self.signInView.onSignEndUI(result: TFSignEnd())
                    }

              
            }else{
                self.TF_toast( error!.msg )
               
            }
        }
    }

   @objc public func replyViewClick()
    {
        self.replyView!.removeFromSuperview()
        self.replyView = nil
        if (self.isOrientationLandscape) {
            self.horChatController!.scorellToreplyCell()

        } else {
            let num = NSInteger(self.scrollView.contentOffset.x/self.scrollView.frame.size.width)
            if (num == 0) {
                self.chatContrller.scorellToreplyCell()
              
            } else if (num == 1) {
                self.questionContrller.scorellToreplyCell()
               
            }

        }

    }
//    #pragma mark -- 课件下载
   @objc public func courseDownloadBtnClick()
    {
        
        // MARK: - 获取下载列表数据
        let param = TFdocParam()
        param.page = 1
        param.accessToken = self.access_token
        self.courseDownView.downLoadRoot.getCourseDownData(param)
        
        self.courseDownView.showCourseDownLoad(true)
    }
    // MARK: 学时防作弊
    public func onPreventCheatingSign(inListener model: TFSignLoopPreventCheat) {
   
        if(self.preventCheatCountdown.remind.isHidden==false)
        {
            self.preventCheatCountdown.remind.isShow = true
            self.preventCheatCountdown.remind.delegate = self
            self.preventCheatCountdown.remind.run(model: model)
            self.preventCheatCountdown.remind.playSound(name:"SuccessfulCheckSoundPop")
            self.view.addSubview(self.preventCheatCountdown)
        }

    }
    
    /**倒计时结束TalkfunPreventCheatCountdownViewDelegate**/
    public func countdownEnds()
    {
        
        /**type | 0: 倒计时结束后发
            1: 继续学习*/
        self.talkfunSDK.preventCheatingSign(inType: 0) { code, error in
            
            
            
        }
        
        
    }
    /**继续学习*/
    public func continueStudying(completion: @escaping (Int, String) -> Void) {
    
        
        self.talkfunSDK.preventCheatingSign(inType: 1) {[weak self] code, error in
            
            guard let `self` = self else { return }
            if(code==0)
            {
                var msg:String = ""
                if(error?.msg != nil)
                {
                    msg = error!.msg
                }
                completion(code,msg)
            }else
            {
                self.view.toast(error?.msg, position: CGPoint(x: UIScreen.main.bounds.width/2, y: self.view.frame.height/2))
            }

            
        }
    }

    public func checkCourseDetail(_ model:TFdoc)
    {
        self.pushCourseDownDetailController = true
        let  vc = TFdocumentPreviewController()
        vc.urlStr = model.url
        vc.localPath = model.localPath
        vc.hidesBottomBarWhenPushed = true
        if self.navigationController != nil {
           self.navigationController?.pushViewController(vc, animated: true)
        }else
        {
            self.present(vc, animated: true)
        }
    }
    //  #pragma mark 控制器的view即将显示的时候调用
        override public func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
        self.pushCourseDownDetailControllerUI(true, color: UIColor(red: 35/255.0, green: 39/255.0, blue: 57/255.0, alpha: 1))
          
        }
    //   #pragma mark 控制器的view即将消失的时候调用
        override public func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
        self.pushCourseDownDetailControllerUI(false, color: UIColor.clear)
            
        }
    public func pushCourseDownDetailControllerUI(_ state:Bool ,color:UIColor)
    {
        if (self.pushCourseDownDetailController == false) {
            
            if self.navigationController != nil {
                self.navigationController?.isNavigationBarHidden = state
                self.navigationController?.interactivePopGestureRecognizer?.isEnabled = !state
            }

            self.statusBar.backgroundColor = color

       }
    }
    // MARK: - 视频播放状态改变  -黑屏加菊花时用到
    public func playerLoadStateDidChange(_ loadState:TalkfunPlayerLoadState) {
        self.updateChrysanthemum()
        if (loadState == TalkfunPlayerLoadState.stalled) {
               print("发生卡顿，显示菊花")
            self.activityIndicator.startAnimating()
            self.activityIndicator.isHidden = false;

        }else
        {
            self.activityIndicator.stopAnimating()
             print("正常播放，移除菊花")
            self.activityIndicator.isHidden = true

        }
    }
    // MARK: - 视频窗口上的菊花
  
    lazy var activityIndicator:UIActivityIndicatorView = {

        if #available(iOS 13.0, *) {
            let act = UIActivityIndicatorView.init(style: UIActivityIndicatorView.Style.white)

            act.isUserInteractionEnabled = false

            return act
        }else
        {
            let act = UIActivityIndicatorView.init(style: UIActivityIndicatorView.Style.whiteLarge)

            act.isUserInteractionEnabled = false

            return act
        }
       
    }()
    public override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent //白色
    }
    // MARK: - 横竖屏支持
    public override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
//        if self.navigationController != nil  {
//            return [UIInterfaceOrientationMask.landscapeRight, UIInterfaceOrientationMask.portrait]
//        }else
//        {
            if self.isOrientationLandscape {
                return UIInterfaceOrientationMask.landscapeRight
            }
            return  UIInterfaceOrientationMask.portrait
//        }
    }
    //#pragma mark - 旋转
    public override var shouldAutorotate: Bool{
       return true
    }
    
    ////隐藏状态栏
    public override var prefersStatusBarHidden: Bool
    {
        if self.isOrientationLandscape {
            return true
        }

        return false
    }

    deinit {
            print("大班直播间没有循环引用")
        }
}
